0
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);

I am using that code, to get the_category(); to exclude some categories. But it ain't working, as it is supposed to...

0

4 Answers 4

7

Try using the filter get_the_categories and replace every occurrence of the_category by echo get_the_category() (just to be sure !)

This is what I cooked:

<?php
add_filter('get_the_categories', 'exc_cat');

function exc_cat($cats) {
        //not on admin pages
        if(!is_admin()){
            $exc = array('lipsum', 'dolor');
            foreach ($cats as $i=>$cat){
                if(in_array($cat->name, $exc)){
                   unset($cats[$i]); 
                }
            }
        }
    return $cats;
}
?>

Try my code and let me know if it does your job.

6
  • The error is the following: syntax error, unexpected T_STRING at line:1
    – Basilakis
    Oct 15, 2011 at 12:34
  • Check my modified answer and code... I've tested it, it works. Oct 15, 2011 at 13:16
  • Hm, check this out... check out the output... It prints the array :O stu.gr/…
    – Basilakis
    Oct 15, 2011 at 13:41
  • Because, I left the print_r statements so that you could see the arrays! No problem, comment them out as I've updated my answer code. Check it now. Oct 15, 2011 at 13:49
  • Hey thanks for the effort. Check it now also, it just outputs the world array! :D
    – Basilakis
    Oct 15, 2011 at 13:52
3

This will work:

Put the following in your functions.php file:

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;
            }
          }
      }
}

and then call the function with:

<?php exclude_post_categories('1,5', ', '); ?>

where 1 or 5 are the category IDs you want to exclude.

Code courtesy of http://wordpress.org/support/topic/the_category-exclude-categories

1
  • +1 This one is interesting because it can be both ways, with or without exclusion.
    – brasofilo
    Jul 24, 2012 at 13:24
0

Enter this handy function from the WordPress support forums — just add it to your theme’s functions.php file:

function the_category_filter($thelist,$separator=' ') {
     // list the IDs of the categories to exclude
     $exclude = array(366);
     // create an empty array
     $exclude2 = array();

     // loop through the excluded IDs and get their actual names
     foreach($exclude as $c) {
          // store the names in the second array
          $exclude2[] = get_cat_name($c);
     }

     // get the list of categories for the current post     
     $cats = explode($separator,$thelist);
     // create another empty array      
     $newlist = array();

     foreach($cats as $cat) {
          // remove the tags from each category
          $catname = trim(strip_tags($cat));

          // check against the excluded categories
          if(!in_array($catname,$exclude2))

          // if not in that list, add to the new array
          $newlist[] = $cat;
     }
     // return the new, shortened list
     return implode($separator,$newlist);
}

// add the filter to 'the_category' tag
add_filter('the_category','the_category_filter', 10, 2);

Initially, I did have some issues with this function; after some tinkering around, I realized I needed a separator declared in the_category() tag, like a comma, dash, etc. In this case, I was using just a space — the_category(' ') — which wasn’t working, but swapping it out for a non-breaking space did the trick: the_category('&nbsp;').

Credit: http://www.stemlegal.com/greenhouse/2012/excluding-categories-from-the-category-tag-in-wordpres/

-1

I have been looking for a solution for something like this and I make some modifications to some code posted in various topics and I get this..

<?php foreach((get_the_category()) as $cat) {
if (($cat->cat_ID=='#')) 
echo $cat->cat_name;
} ?>

where # = your cat ID. I hope it works for you.

2
  • This is not a proper answer. Please add a proper explanation to what your code does and how it works May 30, 2014 at 8:19
  • It displays particular category related to a post.. May 30, 2014 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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