How to Show Random Articles in WordPress

If you are posting actively on your blog you may find like me that your old articles get forgotten and they get less attention from your readers. You could create sticky posts but it’s not a definitive solution, so apart from using a magazine layout, how to revive these articles ?

One answer to this question can be by displaying random posts on your wordpress website. In this quick tutorial we will show you how to display random posts in WordPress as a list.

Display Random posts in wordpress

As stated before you can display random articles with WordPress in several ways. In this example I will show you how to display them as a list that you could use in a WordPress sidebar or in a widget for instance.

To do this, all you have to do is open your sidebar.php with your favorite code editor and paste the following code into it:

<!-- The code below will generate a list of 5 random posts -->

<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>

The parameter “numberposts” is user to chose the number of articles to display. In this example we’ve choosen 5 but you can cahange it to whatever you want.

Rate this post