Display recent posts in WordPress sidebar
November 11, 2007 – 7:46 pm | by Miriam SchwabHere’s a bit of code to put in your sidebar to display the most recent posts on your blog:
<?php $myposts = get_posts('numberposts=10&offset=1');
foreach($myposts as $post) :?>
<li><a href="<?php the_permalink(); ?>"><?php the_title();?></a></li>
<?php endforeach; ?>
You can set the number of recent posts to appear on the first line of code where it says numberposts=10 - change the 10 to the number of posts you want to appear. You can choose to offset the recent posts by any number; for example, if you select offset=1, as we did above, it won’t display the most recent post, but will start the list from the second-most recent post.



26 Responses to “Display recent posts in WordPress sidebar”
By Anna Vester on Nov 11, 2007 | Reply
Nice tip! Thanks for that. I will be implementing this on my blog soon as well.
By Kim on Nov 14, 2007 | Reply
Awesome! Thank you!!!!
By Ryan on Feb 23, 2008 | Reply
I implemented this in a new theme of mine today.
Although "php$myposts" should be "php $myposts"
By Miriam Schwab on Feb 24, 2008 | Reply
Ryan - thanks for the correction! I’m fixing it now.
By Li on Mar 18, 2008 | Reply
oh thanks a lot for your tips!!!!!
I’m not good at php myself so this is totally my saver!!
By vel on Apr 5, 2008 | Reply
hey im just wondering how you would edit the code to be able to put a heading at the top of the list that actually says “Recent Posts” or whatever?
By Ryan on Apr 8, 2008 | Reply
Vel - You just need to add the HTML before the script. Something like the following would work, however the exact layout would depend on the individual sidebar as themes can format their sidebars in many different ways.
<li><h2>Recent Posts</h2></li>
<?php $myposts = get_posts('numberposts=10&offset=1');
foreach($myposts as $post) :?>
<li><a href="<?php the_permalink(); ?>"><?php the_title();?></a></li>
<?php endforeach; ?>
By Phanakapan on Apr 11, 2008 | Reply
I was wondering if there is a way to make this work for posts from a separate wordpress install. I have my blog on a sub URL, but would love to be able to show the post titles in the sidebar on my homepage. thanks for any help you can offer!
By Miriam Schwab on Apr 24, 2008 | Reply
@Phanakapan - To display recent posts from another blog installation, you should use an RSS display method, that publishes your RSS feed from your other site.
By urdubachi on May 21, 2008 | Reply
hi dears i have a this website, i want side bar thumbnails of my posts pictures how can i setup this ………
By Whitney Inkster on Jun 9, 2008 | Reply
Hello Miriam,
Thank you so much for this invaluable code!!!! Wow, exactly what I was working for! Congratulations on a job well done! I am working on a site using this technique - http://www.ilionfilmcompany.com. Keep up the great work and I will visit your site often now…
By Link Building Bible on Jul 12, 2008 | Reply
Exactly what i was looking for to add on one of my other sites. Nice, quick and easy to the point, and customizable.
You’re awesome!
By Jason on Jul 14, 2008 | Reply
The wp_get_archives() template tag already does this:
wp_get_archives(’type=postbypost&limit=10′);
It will print out a bunch of li’s with the title; linked and everything.
By Miriam Schwab on Jul 15, 2008 | Reply
Jason - you’re right, and I can’t remember exactly why I published this bit of code. The only thing I can think of is that this code allows you to offset the recent posts, which is an advantage that the wp_get_archives tag doesn’t offer.
By Miriam Schwab on Jul 15, 2008 | Reply
@Link Building Bible - no, you’re awesome! Thanks for saying thanks.
By Sachin Garg on Jul 16, 2008 | Reply
Thanks, my upgrade to wp 2.6 broke my old code for doing the same job. Your code works like a charm
By Klaus on Jul 23, 2008 | Reply
Thanks
I have implemented this in order to add a class to the <li>-tags
By Martin on Jul 31, 2008 | Reply
Hi Guys,
Thanks for posting this, im having a tiny problem though and Ive been trying for hours to fix it but I cant get it to work, its basically showing the first 10 blog posts on my blog and not the ten most recent, does anyone know why this maybe? this is using the exact code shown above.
By 5ph3x on Jul 31, 2008 | Reply
Thx a lot for this code..really helped my site..applied it here http://bryanking.net/
By Simon on Aug 3, 2008 | Reply
Thanks for the code:) Using it on the 404 page of my blog at <a href=”http://www.blizzardboy.net”>Blizzardboy: a Kiwi in Japan</a>.
By Martin on Aug 6, 2008 | Reply
Just incase anyone has the same error as I previously posted I fixed this by updating the wordpress software to the latest version.
By David Bradley on Aug 20, 2008 | Reply
What do I need to add to make this *not* appear on the homepage but to appear on single posts and cats etc?
By sandi on Aug 31, 2008 | Reply
why its not working in my blog?
By Ben Greenfield on Sep 7, 2008 | Reply
Anybody know how to do this and also automatically include the *date* of each post listed in the recent posts?