Exclude Category from Wordpress Blog Loop & RSS Feed


Dec
4
2009

Wordpress logo If you run a business blog, or you regularly post articles on a specific topic you may be reluctant to post off-topic for fear of losing feed subscribers (although hopefully they will cut you some slack by sticking around).

Before we look at the solution described in the title, the alternative would be to setup a professional blog and personal journal blog for your off-topic and general interest content. If this is not possible due to hosting limitations or you simply don’t have time to setup a separate blog—excluding categories is the answer.

Exclude Category from Blog Loop

Wordpress Categories page First you will need to login to your Wordpress admin panel and navigate to the Categories section (Posts > Categories). Add a new category such as Off Topic, Personal or General as shown in the image.

Once you have created your new category, you need to find the ID for that category. This is fairly simple. Just hover your mouse over the category that should now have appeared on the right-hand side of the Categories page. You should see something like this: http://brightscape.net/blog/wp-admin/categories.php?action=edit&cat_ID=102. On Brightscape the category Personal has ID 102. Let’s assume that is the category that you want to exclude from displaying on the blog loop.

You now need to add the following code to the index.php file which is available in the admin panel (Appearance > Editor > index.php):

<?php
query_posts($query_string . '&cat=-102'); //excludes category ID 102
    //continue regular WP loop
    if (have_posts()) :
      while (have_posts()) : the_post();
          //display posts
      endwhile;
    else :
        //show errors
   endif;
?>

The above code should be placed before < ?php if (have_posts()) : ?> in the index.php file. Test to make sure the category you chose is being excluded from the blog index page. If you haven’t yet posted to the category, test the script on a different category first, and then change the ID number once it is working correctly.

Exclude Category from main RSS Feed

Now you have successfully removed a specific category from the loop, you can also stop that category showing up in your main RSS feed by using the following URL: http://brightscape.net/blog/feed?cat=-102 (Change URL to match your domain and blog location). This code will remove category ID 102 from the main blog feed.

If you use Feedburner make sure you change your source feed to reflect this change otherwise Feedburner will display the feed with the category you want to exclude.

You should now be able to use a category feed for your excluded category as follows: http://brightscape.net/blog/category/off-topic/feed/ (Change URL to match your domain and blog location).

Further reading on the Loop function in Wordpress and credits to Keith Dsouza.

  • Digg
  • Sphinn
  • Propeller
  • StumbleUpon
  • Twitter
  • del.icio.us
  • Facebook
  • FriendFeed

Related Posts



Leave a Reply