我推荐的是WP_Query
用于循环遍历所有自定义post类型post,然后get_posts()
检索每篇文章的附件。下面是一个未经测试的代码段,它可以完成您想要的功能:
// Setup array for storing objects
$my_attachment_objects = array();
// Arguments for custom WP_Query loop
$my_cpts_args = array(
\'post_type\' => \'my_custom_post_type\',
\'posts_per_page\' => 10
);
// Make the new instance of the WP_Query class
$my_cpts = new WP_Query( $my_cpts_args );
// And Loop!
if( $my_cpts->have_posts() ) : while( $my_cpts->have_posts() ) : $my_cpts->the_post();
// arguments for get_posts
$attachment_args = array(
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'post_status\' => null, // attachments don\'t have statuses
\'post_parent\' => $post->ID
);
// get the posts
$this_posts_attachments = get_posts( $attachment_args );
// append those posts onto the array
$my_attachment_objects[$post->ID] = $this_posts_attachments; // make an array with the post_id as the key, just in case that\'s useful
endwhile; endif; wp_reset_postdata();
希望这有帮助。