WordPress: Display Recent, Related or Random Posts in Your Sidebar

You may have noticed that I have dynamic sidebar that changes slightly as you browse the different parts of the site. For example, I have random posts in my home sidebar, while this changes to related posts when browsing a specific post.

One visitor emailed me recently asking me if I could tell her how I managed to display recent, related or random posts in my sidebar.

Well, I neither use a plugin nor SQL statements for this part.

I used the conditional tags to check the current page and to execute the right part of the code.

The Code in Pieces

I divided the code into three pieces, so you can understand it easily.

Recent Posts


<div class="widget">
<h2>Recent Posts</h2>
<ul>
<?php $posts = get_posts('numberposts=3'); foreach($posts as $post) { ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?><br />
<span><?php the_excerpt(); ?><br /><em>&mdash;Posted on <?php the_time('n/j/Y') ?></em></span></a>
</li>
<?php } ?>
</ul>
</div>

The above code picks the latest three posts. You can change the number of posts displayed by changing number in get_posts('numberposts=3'); to whatever you like.

Related Posts by Category


<div class="widget">
<h2>Related Posts</h2>
<ul>
<?php $posts = get_posts('numberposts=3&category='.$category->cat_ID.'&exclude='.$current_post);
foreach($posts as $post) {
?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title() ?>"><?php the_title() ?><br />
<span><?php the_excerpt(); ?><br /><em>&mdash;Posted on <?php the_time('n/j/Y') ?></em></span></a>
</li>
<?php } ?>
</ul>
</div>

We used get_posts('numberposts=3&category='.$category->cat_ID.'&exclude='.$current_post); to make all the business easier. $category->cat_ID pulls the current post’s category, exclude='.$current_post makes sure to exclude the current post from the related ones.

You may use related posts by tag alsoWP Recipes

Random Posts


<div class="widget">
<h2>Random Posts</h2>
<ul>
<?php $posts = get_posts('orderby=rand&numberposts=3'); foreach($posts as $post) { ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?><br />
<span><?php the_excerpt(); ?><br /><em>&mdash;Posted on <?php the_time('n/j/Y') ?></em></span></a>
</li>
<?php } ?>
</ul>
</div>

Our code uses orderby=rand. So WordPress will pick 3 random posts from the database every time our site loads.

Final Thoughts

The above codes can be merged together and displayed on the sidebar using the WordPress conditional tags.

If you have enjoyed this post or have any questions or recommendations, please share your views in the comments section.

19 comments

  1. Dave Nalle says:

    I like the idea of these widgets, but sadly none of them seem to work. All any of them produces in my sidebar is “—Posted on ”

    Dave

  2. Minea Adrian says:

    Please help me, I think at something else, I don’t know how to write the PHP code. I want to add a section in sidebar “My best posts” (5 or 7) random selected URLs from a predefined list of 40-50 URL’s manually added by me in a separate PHP file.

    There must be done with two things:

    1. Line of code from the box <?php wp_get…
    2. PHP file containing the URL list added by me one per line and the little script of course!

    Can you help me?

  3. Jamal says:

    Mikel, I am very glad that you liked it. 😉

    Dave Nalle, please double-check your changes. The code published here is the same I used in my current sidebar.

    Minea Adrian, let me know what type of CMS you are using.

    MK, Sure, you can use random posts based on specific month. Replace this line:

    
    < ?php $posts = get_posts('orderby=rand&numberposts=3'); foreach($posts as $post) {
     // ...
    }
    ?>
    

    …with:

    
    < ?php
    $posts = get_posts('monthnum=8&orderby=rand&numberposts=3'); foreach($posts as $post) {
     // ...
    }
    ?>
    

    Just we added monthnum=8 parameter. This parameter returns posts dated August regardless of the year posted.

    If you want to display posts published in July, 2009 randomly, you need to expand your code like this:

    
    < ?php
    $posts = get_posts('year=2009&monthnum=7&orderby=rand&numberposts=3'); foreach($posts as $post) {
     // ...
    }
    ?>
    

    The above code will return 3 random posts published in July, 2009.

    If you want to make your site more dynamic (e.g. if you want to display random posts from the current month and year), use the code below instead.

    
    < ?php
    $current_time = getdate();
    $posts = get_posts('year=' .$current_time["year"] .'&monthnum=' .$current_time["mon"] . '&orderby=rand&numberposts=3'); foreach($posts as $post) {
     // ...
    }
    ?>
    

    The above code displays 3 random posts published in the current month and year (e.g. as the time of writing August, 2009).

    Good luck.

  4. Joe says:

    I’m just getting the title and ‘- posted on’. Can this be added to my sidebar via a text widget? Or must it be added within the actual sidebar.php file?
    (I’ve added other html links using the text widget but I don’t know if php works in this area) . Thanks. Nice post too btw.

    • Jamal says:

      Joe, PHP code doesn’t work in the HTML/text widget. So you need to open your sidebar.php file and paste your code there.

      Hope it works for you.

  5. eDealz says:

    Hi all,
    I have been trying to get the wordpress recent posts to work on the homepage of my site (not wordpress site) and have failed many times trying with all the code from wordpress and from here too.

    I have added the reuire wp blog header function and used the code within the page to display results. I do not get any errors – however the fact is that i get nothing?

    The site uses smarty – anyone any ideas what the problem could be?

  6. Jamal says:

    eDealz, this is a WordPress code and all the functions are involved in the WordPress core code. So this code has no way to work on non-WordPress platforms.

Comments are closed.