wp_insert_postを使用してJSONをwordpressに挿入しようとしています。私はレンガの壁に当った。私のコードと私のjsonのいくつかを下に見ることができます。JSONからwp_instert_postで投稿を作成するのに手助けが必要
PHP:
<?php
$json_feed = "http://localhost/sample/ss-posts.json";
$json = file_get_contents($json_feed);
$obj = json_decode($json, true);
foreach($obj as $article_array){
$url = $article_array['url'];
$title = $article_array['title'];
$category = $article_array['category'];
$large_summary = $article_array['wp_post_content'];
$post = array(
'post_title' => $title,
'post_content' => $large_summary,
'post_status' => 'publish',
'post_type' => 'post',
'comment_status' => 'closed',
'post_template' => 'content.php'
);
wp_insert_post ($post, $wp_error);
}
?>
JSONデータ:
{
"post":[
{
"title":"Choir to College to Commercial",
"wp_post_title":"Choir to College to Commercial",
"wp_post_content":"My entry into the world of music..."
},
{
"title":"How to Master Vocal Technique",
"wp_post_title":"How to Master Vocal Technique",
"wp_post_content":"Acquiring command over vocal technique is like ..."
},
{
"title":"How Do You Evaluate Singers?",
"wp_post_title":"How Do You Evaluate Singers?",
"wp_post_content":"Many lovers of singing say that they can't put to words ..."
}
]
}
エラー:中/ユーザーforeach()
のために供給無効な引数/アンドリュー/デスクトップ/:このコードを実行しているとき、私は、以下の」警告を受けますnewtest.php on line 6」を参照してください。
if (is_object($obj)){...}
文で全体のforeachループを包むときもforeach((array)$obj as $article_array){...}
なしエラーを追加するエラーなし。
ご迷惑をおかけして申し訳ございません。前もって感謝します!
'のfile_get_contents()は、'、実際に有効なJSONを返していますか? – WillardSolutions
jsonに見られるようにobjに1つのレベルの '投稿'がありません。それで、もしあなたがそうすればうまくいくはずです $ obj = json_decode($ json、true)['post']; – Hassan
$ obj && foreachに[post]を追加した後、元の警告応答に戻ります。警告:/Users/andrew/Desktop/newtest.phpのforeach()で無効な引数が6行目に指定されました –