Most WordPress themes, by default, come with Recent Posts displayed automatically. Depending on the type of blog you run, it is possible that you would prefer to display Recent Posts per category. If this is the case, here is what you need to do to only display recent posts for specific categories.
First, you’ll want to find your Recent Posts code, which is usually found in the sidebar. It will look something like this:
<h2>Recent Posts</h2>
<ul>
<?php get_archives('postbypost', 10); ?>
</ul>
As always, make sure you have a backup of the file in question before making any changes. You’ll then want to replace the above code with the following code:
<ul>
<?php $recent = new WP_Query("cat=1&showposts=10"); while($recent->have_posts()) : $recent->the_post();?>
<li><a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
</ul>
Where is says cat=1, you’ll want to insert the number assigned to the category that you want to display the posts from. You can find this from your Manage -> Categories page. You can also adjust the number of posts to be displayed where it says showposts=10.
This could be useful to someone who wants to display the most recent few posts from a few different categories in their sidebar.













Thanks for the tip.
One more question. How to show posts of various categories excluding the one we have put before, using the code above?
Exactly what I was looking for thanks. its amazing how the wordpress support forum just tells you to go figure it out for yourself, but I can come here and get the answer. you guys rock.
Hi, thanks, works a charm, I wondered how you would go about adding (and controlling the length of) a few lines of the article text to this.
Thanks for this tip. I’ve been trying to figure this out for a couple of months. Does this only work in the sidebar?
Great!!!
Worked first time, tried loads of other tutorial but this was the best by far.
Thanks for your help
I can’t get it to work, if after cat I place the name of the category it still shows me all posts. Instead of placing the name I tried to place a numeric value… but the result is that it shows me nothing if I place 0 and 4, it shows me all posts if I type 1,2 or 3. Can anybody help? thanks
I’ve tried like 5 or 6 different tutorials and none of them worked! — Yours did!
My only question is how can I add a line break between the titles? They are stacked right under each other and a little extra space would be nice! Look, I sell real estate and only dabble at this stuff — forgive my lameness!
Exactly what I was looking for thanks. its amazing how the wordpress support forum just tells you to go figure it out for yourself, but I can come here and get the answer. you guys rock.
Thanks very much. I am an absolute newbie and was working on a readymade template. my client gave me this requirement and you gave me solution. thanks a zillion.
10x man.. needed one with exceprt but this is a good start
Do you know if this would work on a main page rather than the sidebar? I tried it…looked ok on preview but I’m nervous that I’ll break my site. Also is there a way to display an excerpt rather than just the title?
Thanks for your help!
Just what I was looking for. Thanks!
Can this be applicable in any templates?
Thanks for this tip. I’ve been trying to figure this out for a couple of months. I’ve tried like 3 different tutorials and none of them worked! Expect yours.. thanks.
This post tip about “Recent Post” is exceptional. I tried it and it works well with mine. Thanks for sharing this code.
Dreamweaver CS5 tutorial