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>';
}
}
if( !isset( $term_links ) )
return false;
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) ); ?>
EDIT
It was brought under my attention by @manu in comments that if a post has only one term, and term is the excluded term, the functions returns a PHP warning and notice
NOTICE Error: [8] Undefined variable: term_links
and
WARNING Error: [2] join(): Invalid arguments passed
This can be fixed by adding the following code just below the foreach
loop
if( !isset( $term_links ) )
return false;
This will check if $term_links
isset, and if not, it will stop executing and return false
I have updated the original code in the answer, so you can just copy and paste as is