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>—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>—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 also — WP 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>—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.
So cool. I love the related post code, so simple and effective. Thank you very much.
Where this code shall be added?
Accounting Teacher
Usually in the sidebar of your blog
sidebar.php
. Also you can put it anywhere out site the loop.Thanks. This is a great tut.
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
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:
Can you help me?
is it possible to use the random posts but to get a set of posts from a specific month
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:
…with:
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:
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.
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.
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.
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.
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?
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.
Hi just starting on WordPress could you advise on how to get this code working on the sidebar of my main site. Many Thanks
Hey guys I found a quick, free and easy answer to displaying random posts. http://wordpress.org/extend/plugins/random-posts-widget/ I just downloaded that plug in and it displays 5 random post in my side bar. Just thought I would share what i found. Thanks for the great post J Mohamed
Regards,
Dallin
I found this upon, an individual creating this, the idea had been what exactly for you to be searching for!