Exclude a specific tag from the get_the_tags list

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.

3 Answers
3

$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!

Leave a Comment