【发布时间】:2011-09-08 06:01:49
【问题描述】:
所以,我一直在测试 Twitter 的新 Follow buttons,在浏览 ZDNet.com 时,我注意到在他们的作者简介中,每个作者都有关注按钮。有趣的是,按钮会根据作者是谁而改变。这是一个示例:http://www.zdnet.com/blog/btl/sony-predicts-32-billion-loss-following-psn-hacking-japan-earthquake。
我尝试在我的博客 LonePlacebo.com 上复制相同的想法,并取得了一定的成功。
下面的代码是我的作者简介部分,使用了一些 PHP。我使用了一些 if 语句来检查作者,它确实产生了我希望的动态按钮。但是,它也会以纯文本形式两次输出作者姓名。
<?php if ( arras_get_option('display_author') ) : ?>
<div class="about-author clearfix">
<?php echo get_avatar(get_the_author_meta('ID'), 48); ?>
<h4>Written by: <?php the_author(); ?></h4>
<?php the_author_meta('description'); ?>
<!--check if author is Tony -->
<?php if (the_author() == "Tony Hue") : ?>
<a href="http://twitter.com/tonykhue" class="twitter-follow-button">Follow @tonykhue</a>
<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>
<?php endif; ?>
<!--check if author is Joseph-->
<?php if (the_author() == "Joseph Chang") : ?>
<a href="http://twitter.com/ballinacup" class="twitter-follow-button">Follow @ballinacup</a>
<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>
<?php endif; ?>
</div>
<?php endif; ?>
任何帮助将不胜感激。谢谢!
更新我已经尝试更新代码,以便将来有更多的作者。在下面更新的代码中调用 get_author_meta() 会返回 Twitter 字段中作者个人资料中提供的信息。如果作者没有提供任何 Twitter 信息,我希望代码向 @loneplacebo 显示一个默认的关注按钮,但如果他们提供了,则显示一个链接到他们的 Twitter 帐户的按钮。
但问题是,如果没有提供 Twitter 帐户,代码会按预期返回默认按钮。任何想法如何解决这个问题?
<?php if ( the_author_meta('twitter', $current_author->ID) ) : ?>
<!--if no Twitter info provided -->
<a href="http://twitter.com/loneplacebo" class="twitter-follow-button">Follow @loneplacebo</a>
<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>
<?php else : ?>
<!--else, link to author's twitter account-->
<a href="<?php the_author_meta('twitter', $current_author->ID); ?>" class="twitter-follow-button">Follow @tonykhue</a>
<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>
<?php endif; ?>
【问题讨论】:
标签: php wordpress button twitter