Many of us love to show Recent and or Popular Posts of our Blog in sidebar, at the end of a post, etc. And sometimes We do have categories like ‘Misc, Uncategorized, Announcements, etc’. These are some kind of categories we don’t want to display in Recent Posts List. Here I’m sharing a simple code how you can exclude a category (or more) to show up in Recent Posts.
All you need to do is, paste the following code where you want to show your Recent Posts
<ul>
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=5&cat=-XX,-YY,-ZZ');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
In line no.4, showposts=5, 5 is the number of recent posts you want to show. Whereas 3,5 and 7 are category IDs. A minus (-) sign in front of IDs tells the code to exclude them from the list.
Rest of the code is to show Recent Posts. You may need to do some css styling to match your theme.
To use this code in Sidebar, you will need to install PHP Code Widget. This Plugin will let you execute PHP Code in your Sidebar, which is not possible by default.Just install the plugin, add code into PHP Code Widget on Widgets Page and that’s it !



