hide specific categories from showing

I’m trying to hide specific categories from showing up on a portfolio home page.
I was told that this was the concerned line of code on the php template file

<span class="entry-skills"><?php the_terms($post->ID, 'skill-type', '', ', ', ''); ?></span>

but there is no way to exclude certain types from this function by default. I would need to create a custom function in order to do so.

Read More

since I don’t know much about php, I was thinking maybe some kind of expert could help me over here ? thanks

Related posts

Leave a Reply

1 comment

  1. I haven’t tested this, so it might need to be tweaked (specifically, I’m not sure if I got the names of the params right on the term object), but this should get you most of the way there.

    //add filter
    add_filter( 'get_the_terms', 'my_exclude_terms', 10, 1 );
    //function to do filtering
    function my_exclude_terms( $terms ) {
        //only filter for the homepage
        if( is_home() ) { // you may want to use is_front_page(), depending on your settings
            //list the unwanted terms
            $unwanted_terms = array( // fill this array with the unwanted term ids, slugs, or names
                14,
                18
            );
            // loop through the terms
            foreach( $terms as $k => $term ) {
                //only remove term if it's ID or slug is in the array.
                if(
                  in_array( $term->term_id, $unwanted_terms, true ) || //comment out this line to remove term ID checking
                  in_array( $term->slug, $unwanted_terms, true ) ||    //comment out this line to remove slug checking
                  in_array( $term->name, $unwanted_terms, true )       //comment out this line to remove name checking
                ) {
                    unset( $terms[$k] );
                }
            }
        }
        return $terms;
    }
    

    I used the get_the_terms filter because it’s the last filter before the terms are turned to HTML, which makes it significantly more difficult to parse. If you’re a complete novice with PHP and need help troubleshooting or implementing, post a comment. Good luck!