1

I am building a child theme for my personal use. Not very familiar with php, can only do basic things based on logic. I need to exclude one tag from being displayed on the tag list on a single post. Preferably so nothing is displayed at all when this tag is the only tag assigned to the post (I mean, no "Tags" title, nothing).

Here is what I have from the parent theme:

$tags = get_the_tags( $post->ID );
$separator = ' ';
$output = '';
if($tags){
echo '<div class="entry-tags">';
    echo "<p><span>" . __('Tags', 'tracks') . "</span>";
        foreach($tags as $tag) {
            $output .= '<a href="'.get_tag_link( $tag->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts tagged %s", 'tracks' ), $tag->name ) ) . '">'.$tag->name.'</a>'.$separator;
            }
            echo trim($output, $separator);
        echo "</p>";
    echo "</div>";
}

I tried applying this solution, but it didn't work. Probably because I don't fully understand what I'm doing. :)

Could someone maybe help me with this.

0
$tags = get_the_tags( $post->ID );
$separator = ' ';
$output = '';
if($tags){
echo '<div class="entry-tags">';
    echo "<p><span>" . __('Tags', 'tracks') . "</span>";
        foreach($tags as $tag) {
            // dpm($tag) here by uncomment you can check tag slug which you want to exclude
            if($tag->slug != "yourtag"){ // replace yourtag with you required tag name
               $output .= '<a href="'.get_tag_link( $tag->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts tagged %s", 'tracks' ), $tag->name ) ) . '">'.$tag->name.'</a>'.$separator;
            }
          }
            echo trim($output, $separator);
        echo "</p>";
    echo "</div>";
}

You can apply condition that if tagname is not equal to your tag then only it will be added to out put.

Thanks!

  • If the only tag is the one you don't want to show this will still print the div and the span, which in my case contains the visible text "tags". How to get around it? – Michael Rogers Jan 21 at 18:01
1

get_the_tags() uses get_the_terms():

1265            return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) );

Which in turn applies a filter:

1372            $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy );

You can use that filter to exclude the term(s) you wish. The answer is effectively the same as for this question, though the sequence of function calls in Core is slightly different:

function exclude_my_term($terms, $post, $taxonomy) {
  remove_filter('get_the_terms','exclude_my_term',10,3);
  unset($terms[123]); // where 123 is the ID of the term to exclude
  return $terms;
}
add_filter('get_the_terms','exclude_my_term',10,3);
0

I found a similar answer on Stack Overflow, which I think is clearer. To exclude a tag from get_the_tag_list, apply a filter to get_terms:

add_filter('get_the_terms', 'exclude_terms');
echo get_the_tag_list('<p class="text-muted"><i class="fa fa-tags">',', ','</i></p>');
remove_filter('get_the_terms', 'exclude_terms');

Add a filter on get_the_terms and then immediately remove it after echoing.

The callback function removes terms by IDs or slugs:

function exclude_terms($terms) {
    $exclude_terms = array(9,11); //put term ids here to remove!
    if (!empty($terms) && is_array($terms)) {
        foreach ($terms as $key => $term) {
            if (in_array($term->term_id, $exclude_terms)) {
                unset($terms[$key]);
            }
        }
    }

    return $terms;
}

You can do the exact same thing for get_the_category_list - just create a duplicate callback function with a different name - e.g., exclude-cats - and then add the ids you want to exclude and apply the filter:

add_filter('get_the_terms', 'exclude_cats');
echo get_the_category_list('<p class="text-muted"><i class="fa fa-tags">',', ','</i></p>');
remove_filter('get_the_terms', 'exclude_cats');

I suppose you could make this one function and pass in the IDs...

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.