Exclude category from get_the_term_list

Great way to hide categories from the front end if they are reserved for a different purpose:

function get_modified_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '', $exclude = array() ) {

    $terms = get_the_terms( $id, $taxonomy );

    if ( is_wp_error( $terms ) )
        return $terms;

    if ( empty( $terms ) )
        return false;



    foreach ( $terms as $term ) {

        if(!in_array($term->term_id,$exclude)) {

            $link = get_term_link( $term, $taxonomy );

            if ( is_wp_error( $link ) )

                return $link;

            $term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>'; // adjust here to alter appearance of the term on the front end

        }

    }


    if( !isset( $term_links ) )
        return false;

    return $before . join( $sep, $term_links ) . $after;

}

Show the terms in the template:

<?php echo get_modified_term_list( get_the_ID(), 'project_category', '', ' ', '', array(20) ); ?>

Sourced here.

3 thoughts on “Exclude category from get_the_term_list

Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.