WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
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));
                $newlist[] = $cat;
        return implode($separator,$newlist);
    } else
        return $thelist;

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

share|improve this question

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:

add_filter('get_the_categories', 'exc_cat');

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

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

share|improve this answer
The error is the following: syntax error, unexpected T_STRING at line:1 – Basilakis Oct 15 '11 at 12:34
Check my modified answer and code... I've tested it, it works. – Rutwick Gangurde Oct 15 '11 at 13:16
Hm, check this out... check out the output... It prints the array :O stu.gr/… – Basilakis Oct 15 '11 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. – Rutwick Gangurde Oct 15 '11 at 13:49
Hey thanks for the effort. Check it now also, it just outputs the world array! :D – Basilakis Oct 15 '11 at 13:52

This will work:

Put the following in your functions.php file:

function exclude_post_categories($excl='', $spacer=' '){
   $categories = get_the_category($post->ID);
        $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>';
                    $html .= $spacer;
            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

share|improve this answer
+1 This one is interesting because it can be both ways, with or without exclusion. – brasofilo Jul 24 '12 at 13:24

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.

share|improve this answer
This is not a proper answer. Please add a proper explanation to what your code does and how it works – Pieter Goosen May 30 '14 at 8:19
It displays particular category related to a post.. – Pankaj Kumar May 30 '14 at 10:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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