Hiding particular categories on a WordPress blog frontpage
I decided to hide my techie posts (a particular category of posts) from the home page of my blog. The rationale is that most of the people visiting my homepage are friends and family that don’t really want to read the techie stuff, so why should I show those posts on the frontpage? Conversely, most Google visitors are searching for the techie stuff so I still want to leave in on my blog.
I did some googling and found an easy solution, which required a simple tweak to work correctly on my blog. This should work for WordPress 2.7 and most themes…
Step 1. Edit your main WordPress theme’s primary template file, normally index.php
You can edit either in the the theme editor (Appearance ->Editor) or you if you have FTP you can edit the files that way.
Step 2. Find the code similar to:
<?php while (have_posts()) : the_post(); ?>
and below it insert:
<?php if (in_category(YOUR_CATEGORY_ID) && is_home()) continue; ?>
and replace YOUR_CATEGORY_ID with the id number of the category you want to hide. Go into Posts -> Categories and look at the URLs of the categories to find your id numbers, specifically hover over the “edit” link for the category and you should see something similar to this:
http://www.yourblog.com/wp-admin/categories.php?action=edit&cat_ID=32
so in this example the number you would use would be 32.
Here is what you would have in your index.php file:
<?php while (have_posts()) : the_post(); ?>
<?php if (in_category(32) && is_home()) continue; ?>
And to disable/hide multiple categories you can do the following… which will hide any posts with category ID 32, or category ID 9:
<?php while (have_posts()) : the_post(); ?>
<?php if ((in_category(32) | in_category(9)) && is_home()) continue; ?>
I hope this has been helpful to you!
Best regards,
-J.D.
NOTE: Original credit for the single category hiding code goes to http://www.cubeshack.co.uk and though the content doesn’t seem to appear there anymore, it is mirrored on livejournal.