0

这是我打印出来的。

$args = array(
'category' => 'Animation',
'numberposts' => 8
);
$posts_array = get_posts( $args );
echo json_encode($posts_array);

结果如下:(有 8 个像这样相同的 JSON 列表,为方便起见仅显示 1 个。)

{"ID":554,"post_author":"1","post_date":"2012-12-28 19:17:43","post_date_gmt":"2012-12-28 19:17:43"," post_content":"太空竞赛已经开始。随着各国的竞争,我们关注被招募到太空计划中的一只黑猩猩的进展。他的结果可能会证明对全人类的更好的影响。......他会站起来吗对于挑战","post_title":"Alpha","post_excerpt":"","post_status":"publish","comment_status":"open","ping_status":"open","post_password":"" ,"post_name":"alpha","to_ping":"","pinged":"","post_modified":"2012-12-28 19:17:43","post_modified_gmt":"2012-12-28 19:17:43","post_content_filtered":"","post_parent":0,"guid":"http://localhost/Wordpress%203.4.2/?p= 554","menu_order":0,"post_type":"post","post_mime_type":"","comment_count":"0","filter":"raw"

但我只想发送 id 和 post_content。我不断得到空值,无法弄清楚为什么。

4

1 回答 1

1

只需按您需要的字段过滤:(id and the post_contenttitle and permalink

$args = array(
    'category'       => 'Animation',
    'numberposts'    => 8
);

$posts_array = get_posts($args);

$send_array = array();
foreach ($posts_array as $key => $value)
{
    $send_array[$key]["ID"]          = $value->ID;
    $send_array[$key]["post_content"]    = $value->post_content;
}

echo json_encode($send_array);
exit;
于 2012-12-28T23:21:07.917 回答