How To: Display the Recent Posts of Specific Categories
June 7, 2008 by Tony | Tag(s): WordPress Code, WordPress Hacks, WordPress How-To
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.
Related WordPress Posts
- How To: Display A WordPress Tag Cloud On Your Blog (0)
- How To: Using Another Stylesheet For Certain Pages (0)
- WordPress Hack: Creating a Two-Tiered Navigation Menu (0)
- How To: Alternating Your Post Background Colors (0)
- How To: Hiding Your Sub-Categories in WordPress (0)
- How To: Adding Comment Numbers To Your WordPress Theme (0)

![Subscribe to [Blog Tutorials]](http://blogtut.com/wp-content/themes/BlogTutorialPro/images/rss.gif)
![[del.icio.us]](http://blogtut.com/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://blogtut.com/wp-content/plugins/bookmarkify/digg.png)
![[Facebook]](http://blogtut.com/wp-content/plugins/bookmarkify/facebook.png)
![[Google]](http://blogtut.com/wp-content/plugins/bookmarkify/google.png)
![[MySpace]](http://blogtut.com/wp-content/plugins/bookmarkify/myspace.png)
![[Newsvine]](http://blogtut.com/wp-content/plugins/bookmarkify/newsvine.png)
![[Reddit]](http://blogtut.com/wp-content/plugins/bookmarkify/reddit.png)
![[Slashdot]](http://blogtut.com/wp-content/plugins/bookmarkify/slashdot.png)
![[StumbleUpon]](http://blogtut.com/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Technorati]](http://blogtut.com/wp-content/plugins/bookmarkify/technorati.png)
![[Windows Live]](http://blogtut.com/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://blogtut.com/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://blogtut.com/wp-content/plugins/bookmarkify/email.png)






Comments
Feel free to leave a comment...
and oh, if you want a pic to show with your comment, go get a gravatar!