我正在我的首页上建立一个帖子网格,我想每个CPT显示一个随机帖子。目前,CPT是在查询数组中查询出来的:
<?php
$c = 1; //init counter
$bpr = 3; //boxes per row
$wp_query = new WP_Query(array(\'showposts\' => 9, \'orderby\'=> \'rand\', \'post_type\' => array(\'productions\', \'plays\', \'movies\', \'theatres\', \'directors\', \'artists\', \'countries\', \'events\', \'grades\')));
while( have_posts() ) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<p><?php $post_type = get_post_type_object( get_post_type($post) ); echo $post_type->label ; ?></p>
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail(\'thumbnail\');
} else { ?>
<img src="<?php bloginfo(\'template_directory\'); ?>/img/fallback_image.jpg" alt="<?php the_title(); ?>" />
<?php } ?>
</a>
</div>
<?php
if($c == $bpr) :
?>
<div class="clr"></div>
<?php
$c = 0;
endif;
?>
<?php
$c++;
endwhile;
?>
所以这个数组的问题是,当我查询CPT时,它开始重复CPT,有时甚至它会查询CPT中的4篇随机帖子。一种解决方案是创建9个不同的查询—每个CPT一个查询,但这对于首页来说似乎有点过头了。
有没有一种方法可以在不影响页面加载时间的情况下,在每个CPT中只查询1篇随机帖子?