Php 如果满足该div内的条件,如何使用echo来echo div

Php 如果满足该div内的条件,如何使用echo来echo div,php,wordpress,echo,Php,Wordpress,Echo,我有一个简单的php脚本,如果用户在wordpress中的用户帐户字段中提供了他们的社交网络用户名,我将使用它来显示社交图标 问题是,即使未提供网络用户名,也会显示所有网络图标 我清楚地理解发生这种情况的原因,因为所有的div都被包装在同一个echo输出中,但我无法真正弄清楚如果每个div的条件分别满足,如何在该echo中使用isset来输出 我希望我对这个问题的解释不要太混乱 下面是我的脚本示例 echo ' <div class="author_networks"> &l

我有一个简单的php脚本,如果用户在wordpress中的用户帐户字段中提供了他们的社交网络用户名,我将使用它来显示社交图标

问题是,即使未提供网络用户名,也会显示所有网络图标

我清楚地理解发生这种情况的原因,因为所有的div都被包装在同一个echo输出中,但我无法真正弄清楚如果每个div的条件分别满足,如何在该echo中使用isset来输出

我希望我对这个问题的解释不要太混乱

下面是我的脚本示例

    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>'
echo'
'

提前感谢您的建议

我认为您必须对每个图标使用单独的
回音

对每个
使用单独的回音调用:

echo';
如果(isset(获取作者元('google\u plus',$authorID))){
回声“…”;
}
如果(isset(获取作者元数据('facebook',$authorID))){
回声“…”;
}
//等等
回声';

假设
get\u当没有可用的作者数据时,author\u meta()
返回
null
。如果没有,您需要使用另一种方法来检查数据是否可用。

Sime
If
语句将起作用:

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

您需要将其分解为不同的部分,并且应该删除echo并用php之外的html替换它。像这样:

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

对所有4-5个条目都这样做,您就完成了。

这是我刚才使用的帮助我解决问题的方法。这种方法只提供一个社交图标,即使有更多的社交图标。实际上,我使用了您的方法,但对每个div使用单独的echo进行了轻微修改,效果很好。我没有在回声周围使用{},可以不使用吗?知道为什么吗?我意识到有一个括号实际上丢失了,但即使在更正它之后,它仍然给我500个括号,因为它丢失了2个括号,每个
if
-语句中都有一个括号。据我所知,我的更新代码片段不包含任何错误(不再),所以我猜您的更新代码中有什么错误?如果找不到,应该打开一个新问题,提供有关错误的详细信息并提供更新的代码。
?><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>