How to Exclude Category from Post’s Category List

WordPress provides a function that displays the category to which a post belongs: the_category(). If the post is in multiple categories, the function will list them all, linked to the respective archive page.

For some reason, maybe you want to exclude one or two categories for the list. This can be achieved by hacking the_category function, or create a new code to list the category, to replace the_category.

Most of the tutorials I found are using the first, by hacking the_category function. This snippet will do the second one.

<?php
     foreach((get_the_category()) as $category) {
        if ($category->cat_name != 'cat_to_exclude') {
           echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> ';
        }
     }
?>

The code above is to list post’s categories, where category ‘cat_to_exclude’ will be exclude from the list. You can use this code to replace the_category function in your theme.

Hope this snippet helps :)

Our 646-364 exams provide you 100% pass guarantee.You can get access to mcts with multiple prep resources of ccent.

3 Comments to “How to Exclude Category from Post’s Category List”

Add Comments (+)

  1. Justin says:

    Thanks for this. I was having some problems trying to get this to work. I realized I was having a plugin issue that was the root of my problem.

  2. Boorad says:

    How can I use this snippet to exclude multiple categories?

    Thanks!

  3. Thanks for this. I was having some problems trying to get this to work. I realized I was having a plugin issue that was the root of my problem.

Trackbacks/Pingbacks

  1. Exclude Category from Post’s Category List | WP Boss

Leave a Reply

 

Amazingly Beautiful WordPress Themes