duanmao2650 2015-04-22 18:04
浏览 20

在单个get_posts()调用中包含帖子附件(子帖?)

In a wordpress template, I'm doing a quick get_posts() call. I'd like the result to be one array of posts, where each post object in the array contains an array of the post's attachments, (right now I just need the featured image, later I may need more images). The reason: I won't be looping through this result set in a traditional template where I could simply do 'the_post_thumbnail()' - I'm setting up a quick ajax call, so I need one array with all the data to send back to the client.

Can't find much in the docs or online. I've got a working solution, it just feels very inefficient and i'm sure theres a better way...

Herse what I'm doing now:

$ps = get_posts($args);

// ADD POST THUMBNAIL TO POST OBJECT
foreach ( $ps as $post ) : setup_postdata( $post ); 
  $post->img = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); 
endforeach;

// OUTPUT UPDATED ARRAY AS JSON 
header('Content-Type: application/json');
echo json_encode($ps);
die(); 

So again, I just want one get_posts() call which returns the post array, with the attachment IDs included in each post object. Is this possible?

Thanks!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用