这是一个棘手的问题。如果要获取最新的图像附件并显示与其关联的帖子,可以使用:
$attachments = get_posts(\'post_type=attachment\');
$first_attachment_post = get_post($attachments[0]->post_parent);
要获取最新的帖子,但前提是它有附件,我认为唯一的方法是使用两个嵌套查询。例如:
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
$attachments = get_children( array(\'post_parent\' => get_the_ID(), \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\') );
if ( $attachments ) {
// Post output here will only be display if attachments are found
echo \'<li>\' . get_the_title() . \'</li>\';
}
endwhile;