WORDPRESS TIPS AND TRICKS

How to Display Random Posts in WordPress: A Beginner’s Guide

Last Updated: 5 mins By: Sanjip Shah

Do you have a WordPress website? Did you have it for quite a long time? If yes, then you also might have posted on it frequently. When you keep adding new posts on your website, after some time, your older posts might not get sufficient views. It will lead to getting lesser traffic for your website than you deserve. To prevent this, you can add a WordPress random post section on your website. You can add the links for your older posts on this random posts section. Then, your website visitors can easily view them by just clicking on the links.

Importance of Random Posts in WordPress:

WordPress Random posts can be beneficial for your website in many ways. As mentioned earlier, it can be really helpful to boost more traffic for your older posts. You can display their links on any of the sidebars of the home page or any other sections of your webpages. Just make sure that they are easily visible to the website visitors. Then, you can gain a lot of views for them.

The random posts section on your website can also be helpful in enhancing the overall design and functionality of your website. Let’s say you have a lot of blogs or other contents on your website. If you use a full-width layout without sidebar, it might look quite dull. You can use the sidebars on your website to make it more attractive. But, your sidebar equally needs more content to balance the beautiful architecture of your website. You can add your own products, social media links, images, Google Adsense or any other widgets to increase the contents of your website. However, you can also easily add the random posts section for your previous posts on the sidebar.

These WordPress random posts on the sidebars can also help you to gain more SEO score. One of the major ways to increase SEO ranking is internal links. So, adding links to older posts on the random posts section can certainly increase the SEO ranking of the website as well.

Now that we know the importance of the of the WordPress random posts, let’s move on to how to add them.

How to Display WordPress Random Posts?

You can display the WordPress random post section in two different ways. They are:

In this tutorial, we will show you how to add the WordPress random posts using the code as well as plugins from the WordPress Dashboard.

Display WordPress Random Posts Using a Plugin

The easiest way to add WordPress random posts is by using a plugin. This method is also highly recommended if you are a complete beginner to WordPress. All you need to do is install and activate the plugin. We have used the Advanced Random Posts Widget for this tutorial.

advanced-random-posts-widget-display-WordPress-random-post

After you’ve activated it, you need to add the random posts as a widget. Go to Appearance>>Widgets from your WordPress dashboard. Then, drag and drop the Random Post widget to the desired location.

Here, you can also edit the settings for the it like title, number of posts, and so on. Make the necessary changes for it and once you are done and click on Save.

add-widget-display-WordPress-random-post

The random posts can be easily viewed on your website now.

You can also add widgets to your website with the help of the WordPress Live Customizer if you want.

Now that you know how to add the WordPress random posts using a plugin, let’s move on to the method using codes.

Displaying WordPress Random Posts Using Code:

This method is recommended for the ones who have some knowledge about programming or the website backend. Codes can be easily added on the WordPress theme editor files to make the necessary changes.

To display the random posts using codes, you need to locate the functions.php file at first. We’ve used the Zakra theme for this tutorial. In this theme, the function.php is located on the Appearance>>Theme Editor. In the Theme Files, you will be able to see the functions.php file.

The location of the functions.php file may vary from various themes. If you have any difficulties in finding them, please consult your theme support.

Now all you need to do is copy the following code on the functions.php file.

[php] function wpb_rand_posts() {

$args = array(
‘post_type’ => ‘post’,
‘orderby’ => ‘rand’,
‘posts_per_page’ => 5,
);

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) {

$string .= ‘

<ul>’;
while ( $the_query->have_posts() ) {
$the_query->the_post();
$string .= ‘

<li><a href="’. get_permalink() .’">’. get_the_title() .'</a></li>

‘;
}
$string .= ‘</ul>

‘;
/* Restore original Post Data */
wp_reset_postdata();
} else {

$string .= ‘no posts found’;
}

return $string;
}

add_shortcode(‘wpb-random-posts’,’wpb_rand_posts’);
add_filter(‘widget_text’, ‘do_shortcode’);
[/php]

This code generates a shortcode [wpb_rand_posts]. Paste it anywhere you want on the visual editor of your website. Then, all you need to do is click on Update File. It will display and link 5 of the random posts of your website.

copy-paste-code-display-WordPress-random-post

You’ll be able to see them on your website once you’ve updated the shortcode.

display-WordPress-random-post

Wrapping Up:

With the help of this tutorial, we are sure that you will be able to add and display the random posts on your website. They are very important to make your website sustained. The posts can be randomized and different posts can be shown each time the page is loaded. So, each of your older posts has a better probability of getting more traffic.

If you have any questions or suggestions, please comment below. We have also listed some of the articles that you might find interesting.

Scroll to top

Pin It on Pinterest