我正在进行查询,以这种方式在JSON中编码一堆wordpress发布数据:
$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);
但是,如何添加我的JSON还是$ post-> ID和附加图像的url的永久链接?为了得到类似的东西:
{
"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":" "
}
答案 0 :(得分:3)
点击此处:Permalink和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);
正如您在文档中看到的那样,函数get_attached_media返回一个数组,其中包含从指定帖子中选择的所有类型的数据。