如果满足该div中的条件,如何使用echo来回显div

I have a simple php script that I am using to display social icons if user has provided their social network username in the fields of their user account in wordpress.

Problem is that all of the network icons are being displayed even if the networks username was not provided.

I clearly understand the reason why it is happening since all of the divs are wrapped in the same echo output, but cannot really figure out how to use isset within that echo to output if condition of each div is met individually.

I hope that my explanation of the problem is not too confusing

Here is example of my script

    echo '
<div class="author_networks">
<div class="gplus"><a class="soc_btn gplus_img" href="https://plus.google.com/'. get_the_author_meta('google_plus', $authorID) .'" rel="me" target="_blank"></a></div>
<div class="facebook"><a class="soc_btn facebook_img" href="http://www.facebook.com/'. get_the_author_meta('facebook', $authorID) .'" target="_blank"></a></div>
<div class="twitter"><a class="soc_btn twitter_img" href="http://twitter.com/'. get_the_author_meta('twitter', $authorID) .'" target="_blank"></a></div>
<div class="linkedin"><a class="soc_btn linkedin_img" href="http://www.linkedin.com/company/'. get_the_author_meta('linkedin', $authorID) .'" target="_blank"></a></div>
</div>
</div>'

Thanks in advance for your suggestions

Sime IF statement will work:

<?
echo '<div class="author_networks">';
if ($condition == 'gplus') {
    echo '<div class="gplus"><a class="soc_btn gplus_img" href="https://plus.google.com/' . get_the_author_meta('google_plus', $authorID) . '" rel="me" target="_blank"></a></div>';
} elseif ($condition == 'facebook') {
    echo '<div class="facebook"><a class="soc_btn facebook_img" href="http://www.facebook.com/' . get_the_author_meta('facebook', $authorID) . '" target="_blank"></a></div>';
} elseif ($condition == 'twitter') {
    echo '<div class="twitter"><a class="soc_btn twitter_img" href="http://twitter.com/' . get_the_author_meta('twitter', $authorID) . '" target="_blank"></a></div>';
} elseif ($condition == 'linkedin') {
    echo '<div class="linkedin"><a class="soc_btn linkedin_img" href="http://www.linkedin.com/company/' . get_the_author_meta('linkedin', $authorID) . '" target="_blank"></a></div>';
}
echo '</div>';
?>

I think you have to use separate echo per each icon

Use separate echo calls for each <div>:

echo '<div class="author_networks">';
if(isset(get_the_author_meta('google_plus', $authorID))) {
 echo '<div class="gplus">...</div>';
}
if(isset(get_the_author_meta('facebook', $authorID))) {
 echo '<div class="facebook">...</div>';
}
// and so on
echo '</div>';

Assuming that get_the_author_meta() returns null when there is no author data available. If not, you need to use another method to check if the data is available.

You need to break it in separate parts, and you should remove the echo and replace it with html outside php. Like this:

?><div class="author_networks"><?php
if (/*google plus check*/) { ?><div class="gplus"><a class="soc_btn gplus_img" href="https://plus.google.com/<?php echo get_the_author_meta('google_plus', $authorID); ?>" rel="me" target="_blank"></a></div>

Do this for all 4-5 entries, and you're done.