【问题标题】:how to send title and permalink through JSON using get_posts如何使用 get_posts 通过 JSON 发送标题和永久链接
【发布时间】:2012-12-14 03:02:15
【问题描述】:

这是我打印出来的。

$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。我不断得到空值,但不知道为什么。

【问题讨论】:

    标签: wordpress


    【解决方案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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      • 2014-11-28
      相关资源
      最近更新 更多