I’ve use this before:
<?php
foreach((get_the_category()) as $category) {
if ($category->cat_name != 'cat_to_exclude') {
echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> ';
}
}
?>
MichaelH , thx a lot for you help , I see 2 times this code but … where I put the hide category ? I want to exclude the category with the ID number 204 ..
I try many test but nothin :/
Can you tell me where i write the ID , to exclude the category ? 😮
thx alot
@nathanspike: Replace cat_to_exclude
with the actual category ID that you want to exclude and the code should work.
How can you filter out more than one category?
@nathanspike: actually, you’ll have to edit the code slightly to exclude categories by ID. Code is as follows:
<?php
foreach((get_the_category()) as $category) {
if ($category->cat_ID != '204') {
echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> ';
}
}
?>
You need to change “cat_name” to “cat_ID” in order for it to exclude by ID #. I also changed cat_to_exclude to your ID # for ya. Hope that helps!
@sbruner: This probably isn’t the best way to do it, but the following works:
<?php
foreach((get_the_category()) as $category) {
if ($category->cat_name != 'category1' && $category->cat_name != 'category2') {
echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> ';
}
}
?>
Be sure to replace &&
with && when you implement your code (WP forums won’t let me use ampersand inside code tags).
I was looking for some kind of solution like that. Thanks!
I find this solution here, I think it’s work well, and look like very clean!
code to add in the function.php in your theme folder:
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);
if you want to filter more than 2 category, you have to change the last number in the last line of the cove above
take a look here for the add_filter reference