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:

1 <li><h2>Random Post</h2>
2 <ul>
3 <?php $posts = get_posts('orderby=rand&numberposts=5'); foreach($posts as $post) { ?>
4 <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
5 </li>
6 <?php } ?>
7 </ul>
8 </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.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s