我正在使用 Wordpress,对于我主页上的帖子,我希望它们能够随机排序和按名称排序。我的意思是,我想每次在我的主页上显示不同的帖子,但按他们的名字排序。我从我的主题文件中更改了 WP_Query 参数,如下所示,但它不起作用。由于我无法理解的原因,我得到了不相关的结果。
#setup wp_query
$args = array(
'posts_per_page' => $postsperpage,
'orderby' => array( 'rand', 'title' ),
'order' => 'DESC',
);
Run Code Online (Sandbox Code Playgroud)
有没有办法使之成为可能?
ps 老实说,我讨厌那些不相关的问题。如果我在互联网上找到了解决方案,我就不会问这个问题。如果您有合理的理由,请警告我或编辑我的帖子,而不是盲目地拒绝它。
orderby ( string | array ) - 按参数对检索到的帖子进行排序。默认为“日期(post_date)”。可以传递一个或多个选项。
所以:
$args = array(
'orderby' => array( 'rand', 'name' ),
'order' => 'DESC',
);
Run Code Online (Sandbox Code Playgroud)
但我认为这不会让你得到你想要的结果。您很可能只需要使用randwithposts_per_page设置来获得所需的帖子数量。获取所有帖子并在之后按名称对它们进行排序。
例子:
$args = array(
'orderby' => 'rand',
);
// get X random posts (10 by default)
$result = new WP_query( $args );
// sort these posts by post_title
usort( $result->posts, function($a, $b) {
if ( $a->post_title == $b->post_title )
return 0;
return ($a->post_title < $b->post_title) ? -1 : 1;
} );
// Start the loop with posts from the result.
while ( $result->have_posts() ) : $result->the_post();
// do your stuff in the loop
}
Run Code Online (Sandbox Code Playgroud)