Code To Display Recent Posts from Specific Categories In WordPress Blog

Posted by


In many cases we do require to display the recent posts from a specific category… for that we need to modify the sidebar.php file of the current active theme and replace code as this:

Original Code:
<h2>Recent Posts</h2>
<?php get_archives(‘postbypost’, 10); ?>

Replace it with:

<?php $recent = new WP_Query(“cat=4&showposts=10”); while($recent-
>have_posts()) : $recent->the_post();?>
<li><a href=”<?php the_permalink() ?>” rel=”bookmark”>
<?php the_title(); ?>
<?php endwhile; ?>

Where is says cat=4 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.


Shane G.

One comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.