仅当字段已填充时显示链接


Only show link if the field has been filled

在Wordpress网站的作者页面上,我使用此代码显示作者的twitter URL:

<a href=”http://twitter.com/<?php the_author_meta(‘twitter’); ?>” target=”_blank”>Twitter</a>

此代码的问题在于,即使用户没有在其后端配置文件中填写Twitter字段,它仍然会显示链接。如果用户已经填写了他们的twitter,我如何才能使其仅显示

我认为一个基本的PHP IF语句将是解决方案?

简单。您可以使用get_the_author_meta来获取该meta的值(而不是打印该值的the_author_meta)。然后,将其与""(空字符串)进行比较,如果它不是空的,则回显链接,否则好吧,我们没有其他的-它只是不会打印链接。

<?php if(get_the_author_meta('twitter') != ""): ?>
<a href="http://www.twitter.com/<?php the_author_meta('twitter'); ?>" target="_blank">Twitter</a>
<?php endif; ?>