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.












Wednesday, February 6th, 2008 at 8:45 am
Thanks for the tip.
Tuesday, February 19th, 2008 at 12:26 am
One more question. How to show posts of various categories excluding the one we have put before, using the code above?
Saturday, July 26th, 2008 at 11:57 pm
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.