doucheyi1347 2017-05-01 15:56
浏览 32
已采纳

Wordpress json编码永久链接和图像

I'm making a query to encode in JSON a bunch of wordpress post data in this way:

$query = new WP_Query( $args ); 
$posts = $query->get_posts();   
foreach( $posts as $post ) {    
    $output[] = array( 'id' => $post->ID, 'title' => $post->post_title, 'count' => $post->custom_total_hits, 'soundcloud_url' => $post->soundcloud_song, 'soundcloud_id' => $post->soundcloud_ids);
}
echo json_encode($output);

But how can I add to my JSON also the permalink of the $post->ID and the url of the attached image? In order to have something like:

{
"id":28197,
"title":"Hazel English - More Like You",
"count":"000000421",
"soundcloud_url":"https:\/\/soundcloud.com\/hazelenglish\/hazel-english-more-like-you-2",
"soundcloud_id":"317317206",
"link":" ",
"image_url":" "
}
  • 写回答

1条回答 默认 最新

  • doutuo3575 2017-05-01 16:21
    关注

    Look here: Permalink and Attached media

    $query = new WP_Query( $args ); 
    $posts = $query->get_posts(); 
    foreach( $posts as $post ) { 
    $output[] = array( 
    'id' => $post->ID, 
    'title' => $post->post_title, 
    'count' => $post->custom_total_hits, 
    'soundcloud_url' => $post->soundcloud_song, 
    'soundcloud_id' => $post->soundcloud_ids, 
    'link' => get_permalink($post), 
    'images' => get_attached_media('image', $post->ID) );
     } 
    echo json_encode($output);
    

    As you can see in documentation, function get_attached_media return an array with all data of type selected from indicated post.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部