3

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', '', ', ' ); ?>
0

3 Answers 3

7

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

4
  • Nice! It seems that you should define $term_links = array() at the beginning of the function otherwise you will get a PHP warning if all categories happen to be excluded.
    – Manu
    Commented Jan 16, 2015 at 10:19
  • Thanks for that. Will have a look at this as soon as I get time. But again, it would not make sense using the function and excluding all categories Commented Jan 16, 2015 at 10:25
  • Sure — but even if you exclude just one term, you may have a post that has only that term. So when the function runs, $term_links won't be defined, and throws an error when the join() occurs.
    – Manu
    Commented Jan 17, 2015 at 14:09
  • Updated. Many thanks for your input. The code should be "bug-free" for now :-) Commented Jan 18, 2015 at 13:18
1

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);
?>
2
  • Thank you ! but it is not working. Maybe the problem is that I am using the php widget ?
    – jojo79
    Commented Jul 30, 2014 at 12:14
  • This line helped me ` if($term->term_id != 20) { // the term ID you want to exclude`
    – breezy
    Commented Jun 5, 2019 at 18:49
0

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 ( '/ &lt;a href="http://www.myblogname.com/blog/destacados/" rel="tag">Destacados - Portada&lt;/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: / &lt;a href="http://www.myblogname.com/blog/destacados/" rel="tag">Destacados - Portada&lt;/a>)

To know exactly what to delete just watch your generated html code doing before the str_replace and echo $term_list;

1
  • This is an awful way to achieve what the OP is asking. AVOID! Commented Apr 13, 2020 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.