WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I currently have the following trying to avoid showing tags (the icon in this case) when there aren't any, but the icon continues to show up. Any thoughts?

<?php if ( is_singular() && function_exists('the_tags') ) : ?> 
    <p><i class="icon-tags"></i><?php the_tags('', ', ', ' '); ?></p>
<?php endif; ?>
share|improve this question
up vote 5 down vote accepted

Get a string value for the tags and print it only if there are tags:

$tags = get_the_tag_list('', ', ', ' ');

if ( "" !== trim( $tags ) )
    echo "<p><i class='icon-tags'></i>$tags</p>";
share|improve this answer
Your icon is defined in your css for the icon-tags class. So your code still displayed the HTML. @toscho code doesn't echo the HTML unless you have tags. – Stephen Nov 25 '12 at 18:15
@Stephen I made an edit, the HTML should be printed now only if there are tags. – toscho Nov 25 '12 at 18:20
Actually I didn't catch the error. Was referring to the OP; but yes your edit is correct of course. – Stephen Nov 25 '12 at 18:25
You could also use the OP's the_tags('', ', ', ' '); inside the conditional (although it would be redundant). – Chip Bennett Nov 25 '12 at 18:33
Beautiful. Thanks very much. – Christopher Nov 25 '12 at 18:49

you could also add the html into the 'before' parameter of the_tags()

for example reducing your whole code to one line:

<?php if ( is_singular() ) the_tags('<p><i class="icon-tags"></i>', ', ', '</p>'); ?>`
share|improve this answer
+1 For very short strings this is indeed the most pragmatic approach. :) – toscho Nov 25 '12 at 19:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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