Show random posts on front page

⚠️ Code snippets might not be compatible with the latest version of your theme. Always use a child theme to customize your theme, and test your code on a staging website before migrating to production.

You can search the user’s forum or ask the user community by opening a new thread in the Customizr theme forum. Before opening a new topic, you might want to follow the troubleshooting steps..

By default, the latest posts list on the front page gets displayed in reverse chronological order and that is what latest posts is. Can you customize it further?

Can you display random posts on the front page?

Add this small bit of code to your child theme’s functions.php and you can indeed display random posts on your front page.

add_action('pre_get_posts','alter_query');
function alter_query($query){
    if ($query->is_main_query() &&  is_home())
        $query->set('orderby', 'rand'); //Set the order to random
}

 

Leave a comment

Your email address will not be published. Required fields are marked *

6 thoughts on “Show random posts on front page”