Display the Recent Posts of Specific Categories

May 6, 2008 · Print This Article

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.


Your choice for site templates and wordpress themes

Comments

Got something to say?

You must be logged in to post a comment.