Here’s a little function I wrote after facing a similar problem on my blog. It seems to work for me… I’m sure someone can edit and improve it!
Here’s a sample usage:
listCatsAndExcludeSome('21,23,67,8', '<span>', '</span>', ', ');
// List categories and exclude a few
function listCatsAndExcludeSome($excludedCategories, $before, $after, $middle) {
// Get list of desired excluded categories
$excludedCategoriesList = explode(',', $excludedCategories);
$excludedCategoriesListCount = count($excludedCategoriesList);
// Get list of categories from WordPress
$categoryCache = get_the_category();
$categoryCacheCount1 = count($categoryCache);
// Removing unnecessary categories from listing based on passed parameters
for ($i=0;$i<$categoryCacheCount1;$i++) {
for ($j=0;$j<$excludedCategoriesListCount;$j++) {
if ($categoryCache[$i]->cat_ID == $excludedCategoriesList[$j]) {
unset($categoryCache[$i]);
}
}
}
// Re-ordering array-indices and get the size of the array.
// NOTE TO SELF: reset($array) has to do with array pointers and means something else!
shuffle($categoryCache);
$categoryCacheCount2 = count($categoryCache);
$k = 1;
// Now loop through and display the categories
foreach ($categoryCache as $category) {
echo $before.'<a href="https://YOUR_BLOG_URI/'.$category->category_nicename.'" title="All posts tagged with ‘'.$category->cat_name.'’">'.$category->cat_name.'</a>'.$after;
if ($k != $categoryCacheCount2) {
echo $middle;
}
$k++;
}
}