如何避免空字段在php中出现?

时间:2015-05-06 作者:Telstar

请帮助。。。我已经设法做了一些基本的PHP编码来调用单个帖子上的作者元和url信息,但我不知道如何隐藏空的数据字段。

有人能给我一些建议吗?非常感谢

<div id="about-author" class="clearfix"><h3 class="block-title">About <a href="<?php  echo get_author_posts_url( get_the_author_meta( \'ID\' ) ); ?>?rel=author"><font color="#d1004b"><?php the_author(); ?></a></font></h3>
<div class="twocol first"><center><a href="<?php echo get_author_posts_url( get_the_author_meta( \'ID\' ) ); ?>"><?php echo get_avatar( get_the_author_meta( \'ID\' ), 90 ); ?></a><br>Get Social<br>
<a href="<?php the_author_url(); ?>" rel="nofollow" target="_blank"><i class="fa fa-globe" title="Website"></i></a>
<a href="//www.twitter.com/<?php the_author_meta(\'twitter\'); ?>" target="_blank"><i class="fa fa-twitter-square" title="Twitter"></i></a>
<a href="<?php the_author_meta(\'googleplus\'); ?>" target="_blank"><i class="fa fa-google-plus-square" title="Google+"></i></a>
<a href="<?php the_author_meta(\'linkedin\'); ?>" target="_blank"><i class="fa fa-linkedin-square" title="LinkedIn"></i></a></center></div>

1 个回复
SO网友:czerspalace

例如,如果要隐藏,the_author_meta(\'linkedin\'); 如果该字段不存在数据,则可以执行以下操作

<?php if(!empty(get_the_author_meta(\'linkedin\'))) { ?> 
        <a href="<?php the_author_meta(\'linkedin\'); ?>" target="_blank"><i class="fa fa-linkedin-square" title="LinkedIn"></i></a> 
<?php } ?>`
然后将类似的逻辑应用到其他领域。如果这不是你要问的,那么你应该多解释一下你想要实现的目标。

结束