How To Show Random Posts on The Front Page of Your Website?

show random posts

You may have noticed that your WordPress theme is showing the posts according to the date. The latest published posts on the top. The same goes on the front page of your website.

Do you want to show random posts on the front page? When someone visits your website then the front page is the first page to look upon.

The posts would be on the same order you have published. What would you do to show random posts on that page? It’s not only the front page, you can display random posts on any page.

Show Random Posts Just By Modifying The Theme Code.

While developing a WordPress theme, the developers try their best to arrange the order of the articles properly. There would be some rare themes which would have the feature to display the posts randomly.

But, you can edit the theme file easily. I am going to provide you an idea of the code which would be present in your theme files.

Step 1:- If you want to edit the order of the posts showing on the front page then you have to find the “front-page.php” file in your theme. You can find the file from the theme editor. Just go to appearance>>editor and search the file.

If you are using Thesis themes or any other framework which doesn’t let you edit the theme files from the WordPress admin panel, you have to edit the files from the cPanel.

Step 2:- For allowing the theme to show the posts, there would be a code present with conditional statements. Let me show you an example.

<?php
     if ( have_posts() ) {

         while ( have_posts() ) {

           the_post();

           //content here

         }

     }
?>

This is the simple WordPress loop which is used to craft the articles to display. You will find more additional codes emebedded in this loop.

Let me give you another example which would show the posts from the “blogging” category.

<?php
     $BloggingPosts = new WP_Query( array(‘category_name’ => ‘Blogging’, ‘posts_per_page’ => ‘5’));
     if ($BloggingPosts->have_posts()) :
        while ( $BloggingPosts->have_posts()) : $BloggingPosts->the_post();
?>

Though the loop isn’t complete but still, you can get an idea from this. The main code which is showing the posts is the selection of the category.

In this code, an array is used which would pick up the posts from the category name. The number of the posts are also mentioned. You can change both the things.

Step 3:- To show random posts, you just have to remove the code ‘category_name’ => ‘Blogging’ from the array and add the code.

‘orderby’ => ‘rand’

What would you do if you don’t see an array in the loop? Well, every developer has his/her own way of coding.

So, it is possible that your theme consists the arguments in the post loop. If you see an element with “$” sign like $args then you have to modify that.

Step 4:- In the arguments, the same parameters would be passed. The number of posts, post by category name, post by tag, post by category ID, and more. And if you don’t find an argument then the WordPress codex “$query_posts” would be there.

As I have mentioned that it’s not possible to describe the accurate structure of the code because of different coding style. You have to get an idea from these examples.

For an example:

‘category_name=blogging&posts_per_page=5’

If you want to show random posts then you have to edit such type of code. You just have to use it to add the order of the posts. Replace the category with the random posts using the code mentioned below.

‘orderby=rand&posts_per_page=5’

And you are done. The main point is that you have to find the WordPress loop which is showing the posts on the front page or any other page.

After that, place the “orderby” code and save the file. The posts on the page would be showing randomly. Every time you refresh the page, you would see new posts.

I Hope You Can Now Show Random Posts On The Home Page of Your Website.

The above-mentioned are the examples of the WordPress loop which can show the post on your website. Every WordPress theme has different code.

So, you have to look for the basic structure of the loop having “if” statements and “while” loop. If you have a little bit of knowledge about PHP then it won’t be hard for you to understand the meaning of the code.

The “orderby” is the code you have to use. You can give any value choosing from WordPress directory. If you still face any problem then feel free to ask.

by Ravi Chahar

A WordPress Professional and a LinkedIn influencer. A coder by passion and a blogger by choice. WordPress theme development is what he can rock with. He is your WordPress guy who will teach you how to solve WordPress errors, WordPress security issues, theme development, design issues and what not.

2 comments

  1. Hi Ravi. I like the idea of showing posts in random order. From there it shouldn’t be too hard to maybe list the mosts recent post or 2 first then show random posts to fill out the page. I like the idea of highlighting some older posts to get traffic spread around a little more. Or maybe you have 5 categories and you display 1 random post from each category. The options are endless.

    Thanks for sharing this starting point.

    1. Hey Ben,

      There are many options to use. You can show the posts from your one category or maybe two. And the rest can be taken care with the random posts. To leverage your older post, you would get more exposure.

      Most of the people prefer to show one recent post and then use the categories.
      Thanks for stopping by.
      ~Ravi

Leave a Reply

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