Exclude categories from the_category
-
I’ve been trying to use the code below to prevent certain categories from displaying when viewing a post entry. The only issue I have is that the separator (‘, ‘) doesn’t work. Any suggestions appreciated. The code in functions.php is:
function exclude_post_categories($excl='', $spacer=' '){ $categories = get_the_category($post->ID); if(!empty($categories)){ $exclude=$excl; $exclude = explode(",", $exclude); $thecount = count(get_the_category()) - count($exclude); foreach ($categories as $cat) { $html = ''; if(!in_array($cat->cat_ID, $exclude)) { $html .= '<a href="' . get_category_link($cat->cat_ID) . '" '; $html .= 'title="' . $cat->cat_name . '">' . $cat->cat_name . '</a>'; if($thecount>1){ $html .= $spacer; } $thecount--; echo $html; } } } }
The code I use to replace the_category in template files is:
<?php exclude_post_categories('2,6',', '); ?>
An alternative solution I found works but for the fact it excludes child categories when a parent category is excluded. My limited knowledge of PHP is preventing me finding a solution.
Any suggestions appreciated.
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Exclude categories from the_category’ is closed to new replies.