动态 Twitter 关注按钮
因此,我一直在测试 Twitter 的新关注按钮,并且在浏览 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; ?>
So, I've been testing out Twitter's new Follow buttons, and while browsing ZDNet.com, I noticed that in their author bios, they have follow buttons for each author. Interestingly, the button would change according to who the author was. Here is an example: http://www.zdnet.com/blog/btl/sony-predicts-32-billion-loss-following-psn-hacking-japan-earthquake.
I tried copying the same idea on my blog LonePlacebo.com, with moderate success.
The code below is my author bio section using some PHP. I used some if statements to check the author, and it did produce the dynamic button as I was hoping for. However, it also output the author's name twice in plain text.
<?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; ?>
Any help would be appreciated. Thanks!
Update I've tried updating the code so as to allow for more author's in the future. The call to get_author_meta() in the updated code below returns the info provided in the author's profile in the Twitter field. I want the code to display a default Follow button to @loneplacebo if the author has not provided any Twitter info, but if they did, display a button linking to their Twitter account.
Problem though is that if no Twitter account is provided, the code does to return the default button as expected. Any ideas how to solve this one?
<?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; ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
get_the_author()
而不是the_author()
。the_author()
打印姓名,后者返回姓名。Instead of
the_author()
useget_the_author()
.the_author()
prints the name, latter returns the name.