How to remove categories from the_category function in WordPress
You might needed this hack if you want to use a single installation of WordPress for multiple sections. the_category() is WordPress Template Tag that displays a link to the category or categories a post belongs to. If you want to eliminate a specific category from the string returned by the_category, you can use the add_filter() to achieve that.
For Instance you don’t want to show the category Uncategorized and Private, in order to do so first put the paste code at the end of your functions.php located in your current theme folder.
function the_category_filter($thelist,$separator=' ') {
if(!defined('WP_ADMIN')) {
//Category Names to exclude
$exclude = array('Uncategorized', 'Private');
$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);
The above code will exclude the categories you added in $exclude on line 4. Now if you want to eliminate the categories on base of Category ID instead of Category Name (which, in my opinion is a better approach), you’ll need the following code.
function the_category_filter($thelist,$separator=' ') {
if(!defined('WP_ADMIN')) {
//Category IDs to exclude
$exclude = array(1,5);
$exclude2 = array();
foreach($exclude as $c) {
$exclude2[] = get_cat_name($c);
}
$cats = explode($separator,$thelist);
$newlist = array();
foreach($cats as $cat) {
$catname = trim(strip_tags($cat));
if(!in_array($catname,$exclude2))
$newlist[] = $cat;
}
return implode($separator,$newlist);
} else {
return $thelist;
}
}
add_filter('the_category','the_category_filter', 10, 2);
On line 4, you can put the Category IDs you wants to eliminate.
Happy Blogging!
Enjoyed this Post?
Browse Categories
- Browsers (1)
- CSS (1)
- Design (1)
- Development (1)
- Javascript (1)
- Lists (1)
- News and Announcements (1)
- Showcase (1)
- Web 2.0 (3)
- Web Hosting and Domains (1)
- WordPress (5)
- WordPress Hacks (3)
- WordPress Plugins (1)
- WordPress Themes (2)

This is exactly what I was looking for. Thanks
You’re welcome, I am glad that tutorial above was useful for you
Hi Abdullah,
Thanks for the category filter code. ! It saved a lot of mine time !
BR,
Vaidas
Thank you for this tip!
But the_category isn’t the only function that prints categories. There are list of categories displayed on sidebar widget too, and also RSS feeds contain categories for each post.
Do you know way to remove categories from there? I tried all filters from here (plus widget_categories_args found in wp-includes/default-widgets.php) with your function but none worked.
Thanks in advance
(p.s. one tip for your theme here: links are not visible, you should have different style from them)
Unfortunately not work on Wrodpress 2.9. $thelist is not array but string. get_the_category_list function make html string to $thelist. Do you have any ideas?
Hmm, I was excited when I saw this. But I tried this today to “hide” one of my categories, but it’s not working, both on the Category Name and Category ID exclude hacks. I’m on WP 2.9…
@Milan
I will check this and post an update here, thanks for the tip on theme, I will take care of it.
@makoto_kw
. Yes you are right $thelist is a string, thats why I have converted that into array in my code. And another important thing, are you using the_category() to output categories? Because this hack will not work on any other category function.
I am on WP 2.9 and using the same hack
@Vince Lawton,
I am on WP 2.9 and using the same hack and everything is working fine, can you please name the function you are using to output categories?
I have figured out it. It dose not work by the_category(), but works fine by the_category(‘, ‘); Thanks for this tips.
Thanks a lot. It works great on WP 2.9.1
Do you think it shoud be a way to assign different CSS for each category?
I am trying to find a way to have one BOLD categories together with the NORMAL ones…
Thanks again
Very helpful.. Tnx!
A lifesaver ! Thank you
thanks for sharing this!
Question: after making changes to the functions.php file, do I need to do anything special when upgrading? I have never edited anything besides files within my theme, mainly because I was not sure what would happen when upgrading
thanks!
Andy
dont think i checked the notify me w/ new comments. so…am now.
sorry for the double.
Excellent function but I’ve got a question:
The categories are comma-separated but I want them to be list-items. How do I do that?
did anyone tried this on 3.0? it doesn’t work it seems
Very helpful, solved it in about 10 secs. Thnx!