Free Wordpress Blog Setup

How to Display Random Posts in WordPress

By Editorial Staff in Tutorials
How to Display Random Posts in WordPress

Due to the natural structure of blogs, articles end up being buried down once they get old. Most new users join and pick up from the moment they join, and they never bother going back to one of your good posts. One of the easiest way to give a new life to your older posts is by displaying them in the sidebar. In this tutorial, we will share how you can display random posts in your WordPress.

All you have to do is open your sidebar.php, or any other file you like to add it in and paste the following code:

<li><h2>Random Post</h2>
<ul>
<?php $posts = get_posts('orderby=rand&numberposts=5'); foreach($posts as $post) { ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</li>
<?php } ?>
</ul>
</li>

You can customize the styling around it. If you want to display more than 5 posts, simply change the number 5 to the number of your choice.

What Next?

Digg it
Save This Page
Subscribe to WPBeginner
Stumble it
Free Wordpress Blog Setup

Comments

2 Responses to “How to Display Random Posts in WordPress”
  1. helpful post, thanks a lot..

  2. Brandon Cox says:

    By the way, instead of displaying the_title, you can just put “Read a Random Post” and limit it to show only one. Create a nice button and you’ve got a “read a random post button” like you see on some special navigation bars sometimes.

Share Your Opinions

Tell us what you're thinking...
and if you want a pic to show with your comment, then get gravatar!

Please make sure that you have read our Comment Policy.

Due to high volume of request from our readers, we are adding this feature that allows you to stay updated with this post's comments without having to participate in the discussion even though we would love your input as always. Don't worry we hate SPAM just as much as you do, so you will never receive any SPAM messages from our site and that's our promise to you.

Subscribe without commenting

Close Bar