posts - Make categories appear random

I have 5 big categories and there are many small categories in them. If I click on the category menu, I can see the post

I have 5 big categories and there are many small categories in them. If I click on the category menu, I can see the posts in order of date. But I want it to be displayed randomly. How can I make a post appear randomly?

I have 5 big categories and there are many small categories in them. If I click on the category menu, I can see the posts in order of date. But I want it to be displayed randomly. How can I make a post appear randomly?

Share Improve this question edited Nov 2, 2019 at 23:29 butlerblog 5,1213 gold badges28 silver badges44 bronze badges asked Nov 2, 2019 at 17:27 songsong 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

You could hook into the pre_get_posts action hook and set the posts' order to random only for specific categories. Here's a basic example on how to achieve so:

function sort_posts_randomly( $query ) {
    if ( ! is_admin() && $query->is_main_query() ) {
        if ( is_category( 'category id, slug, or name' ) ) { 
            // Sort the posts randomly
            $query->set( 'orderby', 'rand' );
        }
    }
}
add_action( 'pre_get_posts', 'sort_posts_randomly' );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745035660a4607501.html

相关推荐

  • posts - Make categories appear random

    I have 5 big categories and there are many small categories in them. If I click on the category menu, I can see the post

    10小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信