Skip to main content
1 of 2
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209

By default you can't exclude terms from get_the_term_list. However, you can tweak the original function and make it exclude terms. The original function can be found in wp-includes/category-template.php lines 1277 to 1306.

Add this to your functions.php. This new function, as said will exclude any term you specify

<?php
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>';
        }
    }

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

You can change $term->term_id to $term->slug if you need to use the slug instead of the ID. You can even use $term->name if you want to use names

The use is going to be exactly the same as the original function, with one exception, the last parameter which will be an array of term ID's that you want to exclude. Just make sure, if you changed $term->term_id to $term->slug, you'll need to use an array of slugs, not ID's. The same apply for term names.

Here is how you will use the function in your template files

<?php echo get_modified_term_list( get_the_ID(), 'project_category', '', ' ', '', array(20) ); ?>
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209