A passionate programmer’s findings in the world of internet.

Show Your Recent WordPress Posts in SideBar

December 30th, 2005

I've been showing 10 of my most recent posts in my sidebar for quite some time. Some of my friends around me asked me whether I did it with plugin or I wrote the code myself.

I did the code myself.

Here's the code that does the job (add it to your sidebar.php).

<?php query_posts('showposts=10'); ?>
	<h2>Recent Posts</h2>
		<?php while (have_posts()) : the_post(); ?>
		<li><a href='<?php the_permalink() ?>'><?php the_title(); ?></a></li>
		<?php endwhile; ?>

Note that you will need to modify some parts of the code to comply with your template style.

Why am I writing the code when there's plugin? When it's not too hard to do it with a few lines of scripting, I'll prefer to do it myself rather than understanding other's code. At least, I learned something. If I'm not mistaken, I'll have to add a line of code to sidebar even if I use a plugin to do it (it's just my guess, I didn't research on it).

If you notice, I'm showing it on every pages other than the main page. Why? All the recent posts have been shown on the first page! If you would like to do the same, you might need to use is_home() to help you determine whether it is the main page. The normal if-and-else trick will be able to do the job.

Have fun!

If my article helped you solved your problem, consider buy me a beer!

Share this article: del.icio.us | digg it

Related posts:

43 Responses

Elliot Swan says:

And when in doubt, http://codex.wordpress.org is a great resource for stuff like this.

EngLee says:

Yes. Thanks for adding the reference that I’ve left out. 🙂

Edrei says:

Or you can ask the rest of us peeps down at the #wordpress channel at irc.freenode.net. We’re nice and friendly. 🙂

EngLee says:

Wow, I never know that there’s a wordpress channel in IRC! Good.

szehau says:

Great! I used it on my site 😉

szehau weblog - Life, Internet, Online Revenue, Softwares, Gadgets and Programming Experience » Blog Archive » Recent Wordpress Posts in Sidebar says:

[…] I had tried out the code Recent WordPress Post in Sidebar suggested by EngLee. Although the code did shows the recent posts correctly but some functions (eg: is_page() and is_home() are not working properly when it is after the portion of the code. My guess is the wordpress loop in the code modified value of some global variables. […]

garm says:

Nice site. Thank to work…

Tasha says:

thanks for the code.. worked great on my blog!

Velanche says:

Hey right on! I’ve tried a plugin and a few suggestions, but the plugin created a formatting gap on the sidebar. Your code did the trick and it works…thanks very much for sharing it!

will amato says:

Worked like a charm.

A million thanks from this WP newbie.

Chris Coyier says:

Very useful. Thanks man.

Net Writing » Improvements to Sidebar - Wordpress Blog says:

[…] Thanks to Erii for this code: eri’s wordpress blog […]

Mark says:

Thanks for the code and tip!

Jason Liebe says:

Thank you!!

Chris Sainsbury says:

Great code snippet mate! Really useful, thanks 🙂

sara says:

works wonderfully, thank you.

stan says:

thanks, it worked great for me as well on the newest version of wordpress (2.2.1)

Bas Timmers says:

Just one question. I would like to show the last x posts minus the last one. How can I do that?

EngLee says:

Do you mean you don’t want the last post?

The easiest way I can think of, is to use PHP to skip the first one, then show the others. So, you need to query 11 posts if you want to show 10.

fedmich says:

It works, thanks. Gonna apply this on my blog now 🙂

Hans says:

This did not work!! All my links (to pages and posts stopped working!!

What could be wrong?

ethie says:

Thanks! It works! 😀

The Dumber Man says:

Lovely simple code man, works brilliantly, needed to edit a little bit of it to get it to work as i wanted but it worked. Cheers dude

shmoe says:


You are a god, been trying to get this to work on my blog for hours a quick copy paste and it all works!


Frances Palaschuk says:

Worked like a charm, thanks a million!!!

jocuri says:

Thanks! This is just what I been searching for!

Darren says:

You can also just write this:

Much easier 🙂

Darren says:

… the code disappered in my comment, so here we go again (remember the PHP tags):


EngLee says:

I think your way should work. There are many different ways to do it.

Anyway, that’s how I did it in year 2005. WordPress added lots of different functions along the way.

gecedelisi says:

hi, you know this is not efficient.
if you use loop like that in sidebar it effects
the page loop too.

showposts=10 in sidebar and page will 10 posts too.
if this showposts=5 page will 5 posts too.

I think this will be more appropriate

<a href=””>

gecedelisi says:

<a href=””>

gecedelisi says:

<a href="">

gecedelisi says:

<a href=””>

I think this is it 🙂

gecedelisi says:

I couldn’t publish the code 🙂

Atle says:

In the theme that I use, I am including the sidebar before I include the post content. Your piece of code does not work when I include the sidebar before the post content.

Do you have any idea what code I can use in my situation?

– Thanks if you can help me out here 🙂

SuniPeyk says:

Very nice. Me too, dont like plugins if there can be a few lines of code. Thank you for it. 🙂

Ninos says:

How to make it so that it only shows posts for X category?

Lenin says:

hey man.. thanks… u just made my day…

How To’s — Add recent posts to Wordpress sidebar says:

[…] Many thanks to Eng Lee for posting this code on his blog. […]

Brooke says:

How do you show the most recent posts from a certain category? I have three main categories and want to show recent posts from category A, B and C in separate boxes.

ankee says:

Thanks dude for the code. Highly appreciate it.

Andrew says:

Thanks for the code, worked like a charm!

Accounting Teacher says:

You said code should be added in sidebar.php. But at what poit? At the start or end?