Exclude Specific Tags From Showing Up in WordPress Theme

Most WordPress themes display the list of tags under each post. It’s easy to hide all the tags by simply deleting the default WordPress tags function from your theme. However in some cases, we will want to display the list of tags but exclude specific tags from being displayed in the loop. You can do so using the function below.

First we define a function into functions.php to exclude the specific tags. You can use more “case” in the code below to search and disable more tags.

<?php function exclude_tags($tags) { foreach ($tags as $tag) switch ($tag->name) {
case 'foul':
case 'shit':
break;
default:
$newtags[] = $tag;
}
return $newtags;
}
add_filter( 'get_the_tags', 'exclude_tags');
?>

Now paste the code below into your theme where you want to display the tags. For eg: in single.php just below the content.

<?php $posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) { echo $tag->name . ' ';
}
}
?>

That’s it!

Divi WordPress Theme

Leave a Reply

Be the First to Comment!

Leave a Reply


wpDiscuz