Display recent posts in WordPress sidebar
Here’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.
Category: Code Snippets








Nice tip! Thanks for that. I will be implementing this on my blog soon as well.
[...] Display Recent Posts in WordPress sidebar and Hard-coding an SEO friendly title in the WordPress header file – Miriam posted some very simple and powerful tricks. [...]
Awesome! Thank you!!!!
I implemented this in a new theme of mine today.
Although "php$myposts" should be "php $myposts"
Ryan – thanks for the correction! I’m fixing it now.
oh thanks a lot for your tips!!!!!
I’m not good at php myself so this is totally my saver!!
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?
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; ?>
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!
@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.
[...] WordPress Super Loop explains how to exclude posts in a specifica category from the main page. WordPress Garage’s Display Recent Posts in the Sidebar explains how to show recent posts in the sidebar. And finally, we modified the code from WPG to [...]
hi dears i have a this website, i want side bar thumbnails of my posts pictures how can i setup this ………
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…
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!
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.
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.
@Link Building Bible – no, you’re awesome! Thanks for saying thanks.
Thanks, my upgrade to wp 2.6 broke my old code for doing the same job. Your code works like a charm
Thanks
I have implemented this in order to add a class to the <li>-tags
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.
Thx a lot for this code..really helped my site..applied it here http://bryanking.net/
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>.
Just incase anyone has the same error as I previously posted I fixed this by updating the wordpress software to the latest version.
What do I need to add to make this *not* appear on the homepage but to appear on single posts and cats etc?
why its not working in my blog?
Anybody know how to do this and also automatically include the *date* of each post listed in the recent posts?
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?
Thanks for this great tip! It’s saved me a real headache in trying to figure this out for myself.
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.
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!
love this thanks.
is there anyway to get an excerpt of the post with the heading?
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; ?>
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; ?>
[...] WordPressGarage.com Cagintranet Web Design [...]
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
Hey thats awesome, worked perfectly on my page!! yay thanks
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!
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?
thank you so much! i got it work!
Thank you. This post was very useful.
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!
Thanks for your tips…but I don’t where should I put this code in which file…?
Thanks for tips.
but Jason tip really worked for me.
I got exactly what I was looking for. Nice post!!
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 > :$
Thanks a lot Miriam for the amazing tip but could you please tell how to target a specific category?
Thanks,
- Mohammed
@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: code-snippets/display-a-selected-number-of-posts-from-one-category/
Is that what you meant?
[...] WordPress widgets are delightful things where themes designed for public consumption are concerned. There are situations, however, where widgets aren’t in use or not practical/necessary. The code below can be added to your sidebar (or anywhere in your template, really) to display a list of current pages. I found this on an old (2007!) post at WP Garage. [...]
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
@Jamar Why thank you! I don’t think we’ve ever been blessed before on this blog.
Well that is truly a shame sister.
Again thanks.
Peace be with you.
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'); ?]
Thanks!
its working!!!!!
wp_get_archives(‘title_li=&type=postbypost&limit=1′); works for displaying the title of each post, but what about showing the description of each post?
:s
Thanks! I was looking for thist to! It’s working!!!
Hi, thanks for this.
One question: Is it possible to add a “previous posts” lik in the sidebar? And that these previous posts also appear in the sidebar after clicking the link?
Would be great!
One problem with the above code is when you call that function in your sidebar which is called before the start of the loop in single.php. Your single.php file will list down all the articles of your blog post irrespective of the one which is called.
In such cases, you can also use
<?php wp_get_archives(‘title_li=&type=postbypost&limit=10′); ?>
This will list down latest 10 posts in ul, li format. This is bit easier.
How would I add these links to a my sidebar on pages outside of WordPress, but on the same site?
Unfortunately this is not working for me….
I have tried several variants of the above code and its still doesnt work.
The difference is that I’m not using the code into the widget but to the php file.
My current command is
<?php wp_get_archives(‘type=postbypost&limit=5′); ?>
but this it displays the latest 5 posts which are already displayed in full on my home page.
Any help?
Not working
Hi Sir,
I’ve tried to put this code at my footer part, but it doesn’t work for me. Is there any solution? The theme that i using, the side bar is under my post space one.
But i need this code for recent post badly.
Hope to get a solution for my blog. Although my blog is in chinese characters. But you are feel free to take a look at my blog layout.
http://www.fall4lonely.com
I succeed to moved the categories code to my footer part. Hope that i can have recent post as well. =)
The code not working.
Hi, I am needing to put the top 5 latest posts of phpbb in my wordpress theme footer, with this code how do i implement it and where do I link the forum webpage? HELP!! very important asap!
This is the website I will be using it on http://www.makeupadviceforum.com
April Hodge,
Wasn’t that you have that feature at the footer?
I think it is outdated