Display recent posts in WordPress sidebar
November 11, 2007 – 7:46 pm | byHere’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.



51 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?
By Jay Robinson on Sep 17, 2008 | Reply
This is a great tip, and I am using it on the front page of Fountain Hills Update.com. Thanks!
P.s. What would be the easiest way to display recently modified posts?
By Angela on Sep 21, 2008 | Reply
Thanks for this great tip! It’s saved me a real headache in trying to figure this out for myself.
By Ryan on Sep 22, 2008 | Reply
Jay – Displaying your most recently modified posts would be very complicated I think. I assume you would need to start querying the database directly to try and track down the dates on your post revisions.
You would definitely need to be running WP2.6 too, as I’m pretty sure that sort of data wasn’t stored in older versions.
By Kok Choon on Sep 26, 2008 | Reply
Thanks for the code! My theme does not support upper sidebar widget, this code help me to do it!
I just install it on my blog, looks great! Thanks!
By jazr on Oct 23, 2008 | Reply
love this thanks.
is there anyway to get an excerpt of the post with the heading?
By Ryan on Oct 24, 2008 | Reply
jazr – The following script will display a link like the above example does, but should also display an excerpt in a seperate list item below it. I haven’t tested it, but I assume it will work.
<?php $myposts = get_posts(‘numberposts=10&offset=1′);
>
foreach($myposts as $post)
<li><a href=”<?php the_permalink(); ?>”><?php the_title();?></a></li>
<li><?php the_excerpt(); ?></li>
<?php endforeach; ?>
By Ryan on Oct 24, 2008 | Reply
Hmm, it seems this wysiwyg box escapes code automatically, but since I escaped it already it showed my escaped code instead
Here is the correct code:
<?php $myposts = get_posts(‘numberposts=10&offset=1′);
>
foreach($myposts as $post)
<li><a href=”<?php the_permalink(); ?>”><?php the_title();?></a></li>
<li><?php the_excerpt(); ?></li>
<?php endforeach; ?>
By Chris on Dec 19, 2008 | Reply
I just added your code to my blog at http://www.dating-photos.com but how do I change the color of the links? Also, I’d like to get rid of the bullets.
Thoughts
By M on Jan 18, 2009 | Reply
Hey thats awesome, worked perfectly on my page!! yay thanks
By Keith on Feb 19, 2009 | Reply
Thanks! This solved my issue. Starting a blog, Recent Posts are a lot more important to show than Categories (unless you want to show all 2!
I am adding you to the eMarketing Search Engine on my site!
By Craig on Mar 7, 2009 | Reply
Great it works! Thanks! The only problem is my background is red and so I cannot see the text. I have no clue on php what so ever so not sure how to edit the text colour?
By Exam Philippines on Mar 28, 2009 | Reply
thank you so much! i got it work!
By rajasekharan on May 8, 2009 | Reply
Thank you. This post was very useful.
By Darren Jamieson on May 27, 2009 | Reply
Just what I was looking for too. I tried searching for hacks for the widget with no luck, but your code replaced it and made it easy. Cheers!
By daraseng on Jul 15, 2009 | Reply
Thanks for your tips…but I don’t where should I put this code in which file…?
By Vikram on Aug 6, 2009 | Reply
Thanks for tips.
but Jason tip really worked for me.
I got exactly what I was looking for. Nice post!!
By karl on Nov 24, 2009 | Reply
Shenanigans, my browser is displaying an emoticon in part of the code, creating a syntax error and I’m not sure what comes after ($myposts as $post) and before > :$
By Mohammed Alaa on Nov 29, 2009 | Reply
Thanks a lot Miriam for the amazing tip but could you please tell how to target a specific category?
Thanks,
- Mohammed
By Miriam Schwab on Nov 29, 2009 | Reply
@Mohammed Check out this other post here on WP Garage that explains how to display a selected number of posts in the sidebar from a specific category: http://wpgarage.com/code-snippets/display-a-selected-number-of-posts-from-one-category/
Is that what you meant?
By Jamar on Mar 16, 2010 | Reply
Assalaamu Alaikum,
Thank you for sharing this code, it was very helpful and is simple to implement.
May Allah reward you for helping others!
Salam
By Miriam Schwab on Mar 16, 2010 | Reply
@Jamar Why thank you! I don’t think we’ve ever been blessed before on this blog.
By Jamar on Mar 16, 2010 | Reply
Well that is truly a shame sister.
Again thanks.
Peace be with you.
By Joni Mueller on Mar 19, 2010 | Reply
Just FYI, this code works with latest versions of WordPress; I’m using it on my inner pages, http://www.pixelita.com/about-us and so forth:

Substitute [ and ] for left and right carets in code below. The number “10″ can be replaced by however many posts you want displayed; I chose only one, the latest one.
[?php wp_get_archives('type=postbypost&limit=10'); ?]