This morning I needed to use the the_category tag but I needed to exclude a tag via a category name.
<?php
//exclude these from displaying
$exclude = array("Hood Newz");
//set up an empty categorystring
$catagorystrings = array();
//loop through the categories for this post
foreach((get_the_category()) as $category)
{
//if not in the exclude array
if (!in_array($category->cat_name, $exclude))
{
$catagorystrings[] = '<a href="'.get_bloginfo(url).get_option('category_base').'/'.$category->slug.'">'.$category->name.'</a>';
}
}
echo join(', ',$catagorystrings);
?>
If you’re looking to exclude the category across the entire theme through a filter, you can add this to your funtion.php file.
function the_category_filter($thelist,$separator=' ') {
if(!defined('WP_ADMIN')) {
//list the category names to exclude
$exclude = array('Something','Something Else','Blah','YAY');
$cats = explode($separator,$thelist);
$newlist = array();
foreach($cats as $cat) {
$catname = trim(strip_tags($cat));
if(!in_array($catname,$exclude))
$newlist[] = $cat;
}
return implode($separator,$newlist);
} else
return $thelist;
}
add_filter('the_category','the_category_filter',10,2);
Thanks to the WordPress Forums.
