1

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; ?>

2 Answers 2

5

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>";
}
4
  • 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.
    – Steve
    Commented Nov 25, 2012 at 18:15
  • @Stephen I made an edit, the HTML should be printed now only if there are tags.
    – fuxia
    Commented Nov 25, 2012 at 18:20
  • Actually I didn't catch the error. Was referring to the OP; but yes your edit is correct of course.
    – Steve
    Commented Nov 25, 2012 at 18:25
  • You could also use the OP's the_tags('', ', ', ' '); inside the conditional (although it would be redundant). Commented Nov 25, 2012 at 18:33
1

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>'); ?>`
1
  • +1 For very short strings this is indeed the most pragmatic approach. :)
    – fuxia
    Commented Nov 25, 2012 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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