Web design & marketing tips by Ironpaper

How to limit or exclude categories for Wordpress frontpage blog posts

Written by Ironpaper | Sep 9, 2009 8:37:22 PM

We have been asked a few times by bloggers how to limit or exclude categories from your Wordpress installation frontpage. By default Wordpress will display a stream of blog posts from all categories. Some bloggers may wish to not display certain categories in that stream of posts on their homepage. A few web designers asked us how to control or exclude specific categories that should not be displayed.

Our answer was to write the code for the theme, and not edit the core functionality since that would complicate WP updates.

In the index.php file of your theme, there is a Loop statement. Wordpress processes this Loop statement for each of the posts in your blog. The statement begins with this code snippet:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

The loop ends with this piece of code:

<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

Here is one way to solve this problem. This will allow you to limit specific categories from your frontpage and publish the remaining categories automatically. We will try to expand upon this post soon and explain other options and this option in more detail. The numbers "1", "3", "5", and "7" are the categories to be excluded from the frontpage blog.

<?php while (have_posts()) : the_post(); ?>
<?php if (!in_category('1') && !in_category('3') && !in_category('5') && !in_category('7')) { ?>

POST CODE GOES HERE
<? } ?>
<?php endwhile; ?>