我试图编写一个循环,将内容区域中没有附件的帖子排除在外。
我有200个自定义帖子类型的帖子,叫做“学生”。我需要对所有在其内容区域中有图库或附件的学生进行查询。不是他们的特色形象。
这能做到吗?到目前为止,我的循环就是这样。我猜根本不起作用$gotimages
未输出带有附件的帖子。。。
最后的编辑:这里的循环工作得很好,我发现的问题是你只能分配一次图像。这意味着,如果您将图像上载到一篇文章,然后将相同的图像重新用于另一篇文章,则只会显示第一篇文章图像。此循环通过与帖子的关联工作。
为每篇文章重新上传你的图片,使其有效并具有独特的图片**
EDIT: Using the loop from s_ha_dum♦, this is getting closer to the thing I\'m looking for, though it\'s only pulling one result in the loop.
<?php
$args = array(
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'post_status\' => \'inherit\',
\'posts_per_page\' => -1,
\'post_parent__not_in\' => array(0),
\'meta_query\' => array(
array(
\'key\' => \'_thumbnail_id\',
\'value\' => \'x\',
\'compare\' => \'NOT EXISTS\'
)
),
\'fields\' => \'post_parent\'
);
$atts = new WP_Query($args);
$parents = array_unique(wp_list_pluck($atts->posts,\'post_parent\'));
//var_dump($parents);
// get your students posts
$args = array(
\'post_type\' => \'student\',
\'post__in\' => $parents
);
$students = new WP_Query($args);
// lets see some results
//var_dump($students->request);
//var_dump($students->posts);
echo \'<hr />\';
// Looping through the students
// I need to pull a random image from the post\' gallery / attachements
// Unsure of how I do this
if ( $students->have_posts() ) {
while ( $students->have_posts() ) {
$students->the_post();
echo get_the_title();
echo \'<hr />\';
}
} else {
// no posts found
echo \'No students exist\';
}
/* Restore original Post Data */
wp_reset_postdata();
?>