我想得到最后20个帖子(在我的例子中是WooCommerce产品),并以随机顺序显示其中的10个。
现在我得到了这样的新职位:
$args = array(
'post_type' => 'product',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 20,
);我知道我可以得到这样的随机顺序:
'orderby' => 'rand',
'posts_per_page' => 10,但如何将这两者结合起来呢?有办法存储第一个循环中的帖子并在第二个循环中使用它们吗?
发布于 2020-09-06 10:10:15
有几种方法可以做到这一点,这是其中之一
$recent_posts = wp_get_recent_posts( array(
'numberposts' => 20, // Number of recent posts
'post_status' => 'publish', // Show only the published posts
'post_type' => 'product'
));
// array_splice ( array, offset, length )
$sub = array_splice( $recent_posts, 10, 10 );
// Random
shuffle( $sub );
array_splice( $recent_posts, 10, 0, $sub );
// Loop
foreach( $recent_posts as $post ) {
echo $post['ID'] . '<br>';
//echo '<pre>', print_r( $post, 1), '</pre>';
}
wp_reset_query();https://stackoverflow.com/questions/63762872
复制相似问题