Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I always assumed there was a way to exclude certain categories from being output in the loop using get_the_category, the_category, or one of the term related functions. After looking around it seems like the only way is to grab the array and just remove them by ID or name.

For instance this is what I am using:

<?php // exclude category ID 12 as an example.

      foreach((get_the_category()) as $cat) {
        if (!($cat->cat_ID =='12'))
         echo '<a href="' . get_bloginfo('url') . '/category/' 
         . $cat->category_nicename . '/">'. ' | ' . $cat->cat_name . '</a>';
      } 
?>

This looks kinda messy due to having a somewhat "hard-coded url" using /category/ ( I know I can also change this but it still seems counter intuitive).
Is there no better way to exclude categories?

share|improve this question
You can run a filter on list_terms_exclusions to exclude terms site wide, or simply one side or the other(admin or front)... – t31os Jul 26 '11 at 11:20
Ok I tried this but could not get it to work, here are some examples I tried, pastebin.com/j4KKPFKY, any insight? – Wyck Jul 26 '11 at 17:10

1 Answer

up vote 1 down vote accepted

If the URL being "hard-coded" is the problem you can use get_category_link

foreach((get_the_category()) as $cat) {
    if ($cat->cat_ID !='12')
        echo '<a href="'.get_category_link($cat->cat_ID).'"> | ' . $cat->cat_name . '</a>';
}
share|improve this answer
Thanks Bainternet, this does work but after thinking about it I want to remove it from all side wide output (front-end) instead of changing all the templates, I have updated my question. – Wyck Jul 26 '11 at 17:24

protected by toscho Jun 11 '12 at 12:47

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

Not the answer you're looking for? Browse other questions tagged or ask your own question.