Last updated on April 18, 2010. Tags: category page, functions.php, WordPress category ID, WordPress category list
A typical blog post will always provide the category or categories on which that post belongs. Somewhere in the post page, usually under the title but can also be at the end of the post like what I did here in Codeleet, there's a text stating something like, "Posted under Category 1, Category 2, Category 3".
The underlined text are usually links that point towards the category pages (list of all posts under that category). The PHP script that generates a list all the categories where a particular post belong is:
<?php the_category(', ') ?>
If a post is assigned to a subcategory, the parent category isn't automatically included. The comma in the parentheses and quotes indicate the character that separates the categories. You can replace it with some other character as separator. If the post belongs to only one category, this character does not appear.
Now, what if we want to assign posts to one or a few categories that we do not want to be visible in this category list? For example, I want to assign a post under Category 1, Category 2 and Category 3 but I want the site visitors to see only Category 1 and Category 2, with Category 3 being used only for managing the blog. Whatever reason you have for hiding a category from the post category list, this is probably the same reason that you would want to exclude a category from the navigation.
The solution is more complicated than the one we use in the navigation. It's not as simple as adding parameter exclude=1,2 but is nonetheless easy to implement. First, you need to open functions.php and insert the following scripts:
function hidden_category($separator) {
$first_time = 1;
foreach((get_the_category()) as $category) {
if ($category->cat_ID != '8' && $category->cat_ID != '16' && $category->cat_ID != '4') {
if ($first_time == 1) {
echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
$first_time = 0;
} else {
echo $separator . '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
}
}
}
}
As usual, for any scripts that we insert in functions.php, if you're not sure were to insert it, the safest place is immediately after the very first <?php or immediately before the very last ?>.
In the arguments $category->cat_ID != '8', $category->cat_ID != '16' and $category->cat_ID != '4', the numbers 8, 16 and 4 are the category IDs of the categories that you do not want to be visible in the category list. You need to create arguments for each category that you need to hide. If there's more than one category that you need to exclude, you need to separate them with AND operator (&&). Click here to find out how to know the ID of a category.
Next, open the PHP files of your WordPress theme where you can find <?php the_category(', ') ?>. In most WordPress theme, they are the index.php. single.php and if available, search.php. In these files, replace <?php the_category(', ') ?> with the following codes:
<?php hidden_category(', ') ?>
Note that <?php hidden_category(', ') ?> works in the same manner as e <?php the_category(', ') ?> except that the categories indicated in the functions.php are now excluded from the list. The comma between the single quotes and parentheses still indicate the separator between categories and can still be replaced by some other character.
Finally assign a post to one of your hidden categories and see if that category shows up in the category list of that post.
Addendum: 19 April 2010
When you hide a category using the method discussed in this article, hiding a parent category does not automatically hide the child category. For example, a category with ID=22 has child categories with IDs 23 and 24. If you assign posts to either categories 23 or 24, those categories will still be displayed even if you included in the code cat_ID != '22'. You need to separately include cat_ID != '23' and cat_ID != '24' in the hidden_category code to hide them.
Posted by Greten on December 2, 2009 under WordPress tweaks
Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.
* Required. Your email will never be displayed in public.