How to not show tags if the post doesn’t have any?

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

Related posts

Leave a Reply

2 comments

  1. 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>";
    }
    
  2. 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>'); ?>`