How to exclude category from get_the_category?

Status
Not open for further replies.

Divvy

Active Member
806
2009
18
0
Hello,

Can someone please give me a little help here?
I need to exclude a category (ID76) from listing, but I'm too newbie to do this. I already searched for a solution but couldnt do it myself.

Here's my code:

PHP:
<?php $categories = get_the_category();
 if($categories){
?>
 <div id="includes">CATEGORIES</div>
 <?php
foreach ( $categories as $category ) { 
echo '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ><div class="tags">'.$category->name.'</div></a>'; 
}
?>

Thank you guys! :)
 
3 comments
HELLO This is script
Edit you file "functions.php"

Code:
function the_category_filter($thelist,$separator=' ') {      if(!defined('WP_ADMIN')) {  
        //Category IDs to exclude  
        $exclude = array(YOU ID, ID, ID);  
          
        $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;  
    }  
}
 
PHP:
<?php $categories = get_the_category(); if($categories){  ?> 
<div id="includes">CATEGORIES</div> 
<?php foreach ( $categories as $category ) {  
if($category->name !== 'YourCategoryName'){ echo '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ><div class="tags">'.$category->name.'</div></a>'; }
} ?>

if($category->name !== 'YourCategoryName') change YourCategoryName with the name of category that you want to exclude.
 
Status
Not open for further replies.
Back
Top