Home / wordpress tutorials / How to Display Random Posts in WordPress

How to Display Random Posts in WordPress

If you have been running your blog for a long time,displaying random post in wordpress sidebar helps to keep users engaged.

In this article we will be teaching you how to display random post in your blog sidebar.

why display random posts in your wordpress sidebar?

As you already no by  default, wordpress displays the latest blog post in the home page.

One way to show your older post to your blog visitors is to make internal linking an habit. (always link to your older post in a new one, so that users can always see them).

Another way is to display a list of random blog post in your blog sidebar so that users can always see and read your blog older post.

Having said that, now lets see how to display random posts in wordpress

How to display random posts in wordpress

Displaying random posts in wordpress with a plugin: This method is very easy and we normally advice beginners to  always use this method.

First and for most, to display random posts with plugin, you need to install the Advanced Random Posts Widget plugin  from the wordpress directory. for detailed instruction on how to install a plugin click here

After a successful activation of  the plugin, visit  Appearance » Widgets page.

There you will see a widget titled  ‘Random Post’, from the available widgets. for detailed instructions  on what are widgets and how to use it click here

Just drag and drop the widget to your blog sidebar.

Now set the functions of the plugin according to what you desire and click the save button.

advanced random post

Congratulations, check your blog sidebar to see a list of Random post.

Displaying random posts in wordpress without  a plugin: this method requires some coding knowledge, all you have to do is add the code below to your current theme function file.

function goldenspeak_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(‘goldenspeak-random-posts’,’goldenspeak_rand_posts’);

add_filter(‘widget_text’, ‘do_shortcode’);

 

What this function does is to create a list of 5 random post in your wordpress sidebar.

To display the random post, add the shortcode to your sidebar

[goldenspeak-random-posts].

I hope with this you can display a list of Random post in your wordpress sidebar.

 

Check Also

how to set featured image in wordpress

featured image hack in wordpress

One feature of wordpress  is the ability to assign a featured image  to each post. …

2 comments

  1. This is good info. I’ve been looking for some help with wordpress since I’m not a techy person. So l subscribed to this blog.

    • thank you bro for subscribing . if you have any issues you can always send us a message on our facebook fan page and we will do our best to help in the little way we can. please show some love by using any of the share button . God bless you

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.