Connect with us

How to display random posts in WordPress

WordPress

How to display random posts in WordPress

As a blogger, you might want a visitor to stay longer on your site in order to get more page-views. Putting up random posts gives the viewers chance to read more of your content. When navigating your site, a viewer might stumble upon a blog post that might have an attractive title tag leading to the viewer reading it.

Displaying random posts in your blog is one way to reduce bounce rate on your blog. Bounce rate is the number (percentage) of visitors who navigate away from your site after viewing a page. Now do you understand what I mean by random posts is one way to reduce bounce rate on your blog? In this article, we are going to take a look at how we can display random posts in WordPress.

How to display random posts in WordPress

There are two ways to display random posts in WordPress, although one seems easy and the other will seem a little bit difficult but I will try my best to be specific.

1. Using a Plugin

This is the first and easier method. This method is recommended for most users.

  • First of all, install and activate Random Posts Widget. For more information, see our post on how to install plugins on WordPress.
  • Upon activation, go to Appearance>> Widgets.
  • There will be a new widget title, “Random Posts” among the list of available widgets.How to display random posts in WordPress
  • Simply hold the widget and add it to a sidebar.
  • Do not change anything in the settings, just save.

2. Using PHP function

  • Simply copy and paste the code below in your theme’s functions.php file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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');

 

The code above, creates a function that displays 5 random posts. It also enables short-codes inside WordPress widgets that enables you to display random posts inside a text widget.

You might wonder why there isn’t a random post widget on this blog. As we all know, this blog uses WordPress platform and it is not common for “someone to teach what he does not practice”. The main reason why I’ve not started using this is because I’m still working on some of my posts. Enjoy!

Continue Reading
Advertisement

Get the latest technology news, games, apps, web tutorials, phone specifications, blogging tutorials, affiliate programs, computer tips and so on

Click to comment

Leave a Reply

Your email address will not be published.

More in WordPress

To Top