Wordpress:从JSON Feed中插入帖子

时间:2014-03-22 09:46:28

标签: php arrays json wordpress

我的二十四个主题目录中有一个文件: data.php ,该目录从JSON提要接收数据,然后将其输入到wordpress数据库。

该文件如下:

<?php
require(dirname(__FILE__)."/../../../wp-config.php");

$json_feed = "http://digitalrand.net/api/url_data/?key=********&pass=********%";
$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['summary'];
    $sum = implode(',',$large_summary);

    $post = array(
        'post_title' => [$title],
        'post_content' => [$sum],
        'post_status' => ['publish'],
        'post_type' => ['post'],
        'comment_status' => ['closed'],
        'post_template' => ['content.php']
        );

    wp_insert_post ($post, $wp_error);
}
?>

上面的代码抛出一个错误:警告:strtolower()要求参数1为字符串,数组在第979行的C:\ wamp \ www \ abacus \ wp-includes \ formatting.php中给出

我希望数据在收到后立即输入wordpress db,以便它可以包含在循环中并显示在我网站的主页上。

1 个答案:

答案 0 :(得分:1)

$post = array(
        'post_title' => [$title],
        'post_content' => [$sum],
        'post_status' => ['publish'],
        'post_type' => ['post'],
        'comment_status' => ['closed'],
        'post_template' => ['content.php']
        );

不正确,应该是

$post = array(
        'post_title' => $title,
        'post_content' => $sum,
        'post_status' => 'publish',
        'post_type' => 'post',
        'comment_status' => 'closed',
        'post_template' => 'content.php'
        );

点击此处查看完成情况

https://codex.wordpress.org/Function_Reference/wp_insert_post