我已经将下面的代码设置为显示自定义帖子类型的最新两篇帖子的信息(它还循环并添加一类first-to-alternative-items,以便于布局)。我该如何修改它以显示两个随机帖子?
<?php
$counter = 1;
$args = array( \'post_type\' => \'custom_advert\', \'posts_per_page\' => 2 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo \'<div class="sixcol \';
if ( $counter % 2 == 1 ) { echo \'first\'; }
echo \'"><a href="[using custom meta to get link address here]"><img src="[using custom meta to show image here]"></a></div>\';
$counter++;
endwhile; ?>
最合适的回答,由SO网友:s_ha_dum 整理而成
你需要一个orderby
argument.
$args = array(
\'post_type\' => \'custom_advert\',
\'posts_per_page\' => 2,
\'orderby\' => \'rand\'
);
这应该以随机顺序拉动帖子,并在检索前两个帖子后停止,因此是两个随机帖子。