On a recent WordPress website project I was working on, I needed to remove specific categories from the list of categories that the “get_category()” function returns. Carl Bliss discovered the solution on the Technokinetics blog.
First, add the following to functions.php:
<?php //Incomplete Category List to replace get_category() //Adapted from: http://www.technokinetics.com/exclude-categories-from-the_category/ function incomplete_cat_list($separator) { $first_time = 1; foreach((get_the_category()) as $category) { if ($category->cat_name != 'CATEGORY TO EXCLUDE' && $category->cat_name != 'ANOTHER CATEGORY TO EXCLUDE') { 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>'; } } } } ?> |
Make sure to replace the “CATEGORY TO EXCLUDE” text with your category’s nicename.
Then add the following to your theme:
<?php incomplete_cat_list(', ') ?> |
That’s it!
//exclude categories with id 1 and 3
$args = array(‘exclude’ => array(1,3));
$categories = get_categories( $args );