在我的 Wordpress 网站的作者页面上,我使用此代码显示作者的 twitter URL:
<a href=”http://twitter.com/<?php the_author_meta(‘twitter’); ?>” target=”_blank”>Twitter</a>
这段代码的问题在于,即使用户没有在他们的后端配置文件中填写 Twitter 字段,它仍然会显示链接。我怎样才能让它只在用户填写他们的推特时才显示?
我认为一个基本的 PHP IF 语句将是解决方案?
简单的。您可以使用它get_the_author_meta来获取它的值meta(而不是the_author_meta打印值)。然后,将其与""(empty string) 进行比较,如果它不为空 - 回显链接,否则....,我们没有其他 - 它只是不会打印链接。
<?php if(get_the_author_meta('twitter') != ""): ?>
<a href="http://www.twitter.com/<?php the_author_meta('twitter'); ?>" target="_blank">Twitter</a>
<?php endif; ?>