I want to exclude one of the categories from my posts, which will always be the same, named "documentaires" and with the ID 20.
Here is the code :
<?php echo get_the_term_list( get_the_ID(), 'project_category', '', ', ' ); ?>
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) ); ?>
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
$term_links = array()
at the beginning of the function otherwise you will get a PHP warning if all categories happen to be excluded.
$term_links
won't be defined, and throws an error when the join()
occurs.
Try this
<?php
$terms = get_the_terms( the_ID(), 'project_category' );
if(!empty($terms)) : ?>
<?php
foreach ( $terms as $term ) {
if($term->term_id != 20) { // the term ID you want to exclude
$term_IDs[] = $term->term_id;
}
}
$terms = implode(',', $term_IDs);
?>
Easier that that! Here is an easy way: It works perfectly for me:
$term_list= get_the_term_list( $post->ID, 'post_type_name', "", " / ");
echo str_replace ( '/ <a href="http://www.myblogname.com/blog/destacados/" rel="tag">Destacados - Portada</a>' , "" , $term_list);
What I do is: Search the term list string and delete with a string replace function exactly the link i want (in this case: / <a href="http://www.myblogname.com/blog/destacados/" rel="tag">Destacados - Portada</a>)
To know exactly what to delete just watch your generated html code doing before the str_replace
and echo $term_list;