我想显示一个功能的帖子,从我最近的3个帖子随机。我已经成功地启动了它并运行,随机挑选任何帖子都很好,但我想把它过滤到最新的3。
'post',
'orderby' => 'rand',
'posts_per_page' => 1,
'post_status' => 'publish'
);
$rand_query = new WP_Query( $args );
if ( $rand_query->have_posts() ) :
while ( $rand_query->have_posts() ) : $rand_query->the_post();
?>
// DIV FOR SINGLE FEATURED POST HERE // etc...
显然,如果我将posts_per_page更改为3,我就会得到包含特色post预览的3个div。我只想从最后的3篇文章中随机挑出一篇文章。日期查询不起作用,因为这些帖子是不定期的。
发布于 2018-07-13 03:06:26
我的方法是..。
首先你必须选择3个最新的帖子,然后你要随机选择其中的一个.
但是,对选定的帖子进行洗牌比只选择其中一个要容易一些--这样你仍然可以使用普通循环:
'post',
'posts_per_page' => 3,
'post_status' => 'publish'
);
$rand_query = new WP_Query( $args );
shuffle( $rand_query->posts );
if ( $rand_query->have_posts() ) :
while ( $rand_query->have_posts() ) : $rand_query->the_post();
?>
// HERE GOES THE DIV WITH POST
https://wordpress.stackexchange.com/questions/308425
复制