Hide a Specific Tag from a Post in WordPress

Wordpress, Website Design

General Graphic: Code Photo by Negative Space

One of the things that drives me nuts about WordPress’ taxonomy (tag) system is that you have no way to prevent your readers from viewing all of the tags you used on a post. Sometimes I just want particular tags to go away because I’m using them for internal purposes (either for quick internal links or for logic based functionality my themes are using for display purposes.) For example, if you wanted to display an affiliate disclaimer message only on posts with a specific tag but don’t necessarily want to show that entire tag to the reader in the post meta information.

So how do you hide a specific tag from a post? Well, you can install yet another plugin… but I personally don’t like to install new plugins when I can avoid it. Here’s how you can stop displaying specific tags on posts in WordPress without a plugin. It’s as simple as adding a few extra lines to your functions.php and a single line to your template file!

Keep a Specific Tag from Displaying on a WordPress Post

Step 1:
Make a backup of your theme’s functions.php and your single.php (or other template file) where you will use this function. Always make a backup before you begin!

Step 2:
Open your functions.php in a code or text editor, scroll to the bottom, and paste the following code:

//* Hide Specific Tags from the Post
//* ---------------------------------------------------------------------------
function show_tags_except_hidden() {

  $hide_tags = array( 'tag-slug-to-hide' );
  $tag_open_wrapper = '<div class="entry-tags">';
  $tag_close_wrapper = '</div>';
  $tag_title_before_name = "More Posts with the '";
  $tag_title_after_name = "' Tag";
  $separator = ' ';

  // Do not edit below this line
  // ---------------------------------
  $tags = get_the_tags( $post->ID );
  $output = '';
  $display_result = $tag_open_wrapper;
  if( $tags ) {
    foreach( $tags as $tag ) {
      $should_display_tag = true;
      foreach( $hide_tags as $hide_tag ) {
        if( $tag->slug == $hide_tag ) { $should_display_tag = false; }
      if ( $should_display_tag === true ) {
        $output .= '<a href="'.get_tag_link( $tag->term_id ).'" title="'.esc_attr( sprintf( __( '%s%s%s', 'theme' ), $tag_title_before_name, $tag->name, $tag_title_after_name ) ) . '">'.$tag->name.'</a>'.$separator;
    $display_result .= trim($output, $separator);
  $display_result .= $tag_close_wrapper;
  return $display_result;
//* ---------------------------------------------------------------------------

Step 3:
Modify the values of the $hide_tags array to include all the tag slugs that you want to be hidden when this list is displayed. This is the only value you really need to change.

If you want to modify the other variables in this section to match your theme and/or coding style, here is a detailed list of what each variable is and what it does.

  • $hide_tags – A comma-separated array of the slugs for each tag you want to hide.
  • $tag_open_wrapper – The opening container element that wraps all of the tag links.
  • $tag_close_wrapper – The closing container element that wraps all of the tag links.
  • $tag_title_before_name – The text to display in the link title before the tag name.
  • $tag_title_after_name – The text to display in the link title after the tag name.
  • $separator – Anything you want to show up between tag links.

Step 4:
Open your single.php in a code or text editor. In your code, find wherever you want to display your tag cloud (typically within a post meta section), and paste in the following code. This is display the results of the function we added in Step 2.

<?php echo show_tags_except_hidden(); ?>

Step 4:
Upload your modified functions.php and your single.php (or other template file) and test!

An Example of Hiding a Specific Tag

So for example, let’s say if you had a post that was tagged with tag0, tag1, and tag2. When you modify the function so the array in variable $hide_tags includes the slug for tag1, the displayed result will be HTML code that looks something like this:

<div class="entry-tags">
   <a href="http://yourdomain/tag/tag0/" title="More Posts with the 'tag0' Tag">tag0</a>
   <a href="http://yourdomain/tag/tag2/" title="More Posts with the 'tag2' Tag">tag2</a>

It doesn’t matter what order your tags are in, it doesn’t matter if you just want to hide a single specific tag or hide multiple tags. Whatever tags slugs you include in that array will be stripped from the display before it shown in your post meta info.

Why Would you want to do this?

In my themes, I use a lot of logic to display particular elements.

That’s it! If this article has been helpful to you, please share it with others!

You may also like