将WordPress特色图像添加到JSON编码阵列

时间:2014-08-19 18:52:48

标签: json wordpress

我使用WP_Query和json_encode在JSON数组中返回WordPress帖子数据,这样我就可以使用Angular.js作为前端。一切正常,但我无法将特色图像包含在JSON文件中。 这是我的代码:

<?php 

function test_ajax()
{
    header( "content-type: applications/json" );
    $query = new WP_Query( array( 
        'numberposts' => 10,
        'order'=> 'DESC',
        'orderby' => 'title',
        'post_type' => 'post',
        'meta_query' => array(
        array( 'key' => '_thumbnail_id')
    ) ) );
    echo json_encode($query->get_posts());
    die();
}
add_action("wp_ajax_nopriv_test_ajax", "test_ajax");
add_action("wp_ajax_test_ajax", "test_ajax");

这是输出的json:

[{
"ID":133,
"post_author":"1",
"post_date":"2014-08-19 05:54:03",
"post_date_gmt":"2014-08-19 05:54:03",
"post_content":"\"Woman",
"post_title":"Woman on the Banks of the Seine, After Courbet, 1950",
"post_excerpt":"",
"post_status":"publish",
"comment_status":"open",
"ping_status":"open",
"post_password":"",
"post_name":"woman-on-the-banks-of-the-seine-after-courbet-1950",
"to_ping":"",
"pinged":"",
"post_modified":"2014-08-19 05:54:03",
"post_modified_gmt":"2014-08-19 05:54:03",
"post_content_filtered":"",
"post_parent":0,
"guid":"http:\/\/localhost\/mabs\/?p=133",
"menu_order":0,
"post_type":"post",
"post_mime_type":"",
"comment_count":"0",
"filter":"raw"
}]

0 个答案:

没有答案