developer developer - 1 year ago 65
PHP Question

How to hide the font awesome icon when the text next to it is not displayed?

I am trying to show the font awesome icons next to some metadata for my Wordpress posts. An example is the use of the tag icon before the text 'tag' in the following code:

<i class="fa fa-tags" aria-hidden="true"></i> <?php eco($tag); ?>

The problem is I need to hide the FA icon when the tag name is not shown. I mean if the user decides not to write any tags, the icon sticks there.

Any ideas on how I can get rid of the icon when no tags are written for a post?

The complete code is:

<div class="meta-tags">
<?php $tag = get_the_tag_list( __('tags: ', 'themename'),', ' );
<i class="fa fa-tags" aria-hidden="true">&nbsp;</i> <?php echo ($tag); ?>


Answer Source

I prefer to use the emptyfunction. And you can merge all pieces of your php-code.

<div class="meta-tags">
     $tag = get_the_tag_list( __('tags: ', 'themename'),', ' );
     if ( !empty($tag) ) echo '<i class="fa fa-tags" aria-hidden="true">&nbsp;</i>' . $tag; 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download