2

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

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

| improve this answer | |
  • 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 Jan 16 '15 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 – Pieter Goosen Jan 16 '15 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 Jan 17 '15 at 14:09
  • Updated. Many thanks for your input. The code should be "bug-free" for now :-) – Pieter Goosen Jan 18 '15 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);
?>
| improve this answer | |
  • Thank you ! but it is not working. Maybe the problem is that I am using the php widget ? – jojo79 Jul 30 '14 at 12:14
  • This line helped me ` if($term->term_id != 20) { // the term ID you want to exclude` – breezy Jun 5 '19 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;

| improve this answer | |
  • This is an awful way to achieve what the OP is asking. AVOID! – Alexander Holsgrove Apr 13 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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