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?

  • 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

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>';
  • 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 fuxia Jun 11 '12 at 12:47

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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