How to hide some categories in category list under post in wordpress?

In My wodpress Blog there are some categories use for the internal work. I want to hide that categories in blog homepage category list show under each post.

I print category list as print(the_category($postID));

Read More

How to hide or make filter for that category list print under each post?

Related posts

Leave a Reply

1 comment

  1. I’m afraid that in this case you will have to go throught the categories and have some logic here.
    you can use a function like this one:

    function filterCategories($postId,$excluded, $separator){
        $categoriesToDisplay = '';
        $count = 0;
        $allCategories = get_the_category($postId);
        foreach($allCategories as $category) {
            if ( !in_array($category->cat_ID, $excluded) ) {
    
               // get that category link
               $cat_link = $get_category_link($category->cat_ID);       
               $categoriesToDisplay .= $cat_link;
    
               // we want a separator, but not at the end...
               if($count < count($categories)){
                   $categoriesToDisplay .= $separator;
               }
            }
            $count++;
        }
            return $categoriesToDisplay;
    }
    

    It will trancate the categores you want without the excluded as links seperated by what ever you want (in this case “|”)
    You can call it within your loop:

         <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
         <?php $id = get_the_ID(); ?>
         <?php echo filterCategories($id, array(1,2,3,4)," | "); ?>
    

    $categories array will be the array of categories objects what you can then display as:

    I haven’t tested it, but it should work.

    To get the excluded categories you can use a parent category called “excluded” or something like that. than they can be available for you easily:

    $excluded = wp_list_categories("child_of=".$excludedCategoriId);
    

    Hope it helps.