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":" "
}
我正在进行查询,以这种方式在JSON中编码一堆wordpress帖子数据: p>
$ query = new WP_Query($ args);
$ posts = $ query-> get_posts();
foreach($ posts as $ post){
$ output [] = array('id'=> $ post-> ID,'title'=> $ post-> post_title,'count'=&gt ; $ post-> custom_total_hits,'soundcloud_url'=> $ post-> soundcloud_song,'soundcloud_id'=> $ post-> soundcloud_ids);
}
echo json_encode($ output);
代码> pre>
但是如何将我的JSON添加到$ post-> ID的永久链接和附加图像的url? 为了得到类似的东西: p>
{
“id”:28197,
“title”:“Hazel English - More Like You”,
“count” : “000000421”,
“soundcloud_url”: “HTTPS:\ / \ / soundcloud.com \ / hazelenglish \ / hazelenglish-比较像你-2”,
“soundcloud_id”: “317317206”,\ n“link”:“”,
“image_url”:“”
}
code> pre>
div>
答
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.