2011-06-04 171 views
0

因此,我一直在测试Twitter的新Follow buttons,在浏览ZDNet.com时,我注意到在他们的作者bios中,他们按照每个作者的按钮。有趣的是,按钮会根据作者是谁而改变。这里是一个例子:http://www.zdnet.com/blog/btl/sony-predicts-32-billion-loss-following-psn-hacking-japan-earthquake动态Twitter关注按钮

我试着在我的博客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信息,我希望代码显示一个默认的Follow按钮给@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; ?> 

回答

2

代替the_author()使用get_the_author()the_author()打印名称,后者返回名称。

+0

谢谢dpacmittal!我忘了get_the_author() – Tony 2011-06-04 05:16:08

+0

@tony如果它是正确的,一定要接受dpacmittal的回答,如果它是特别有用的,请提高它的回答。 – arcain 2011-06-04 05:41:29

+0

@Tony对于更新部分,你又犯了错误。使用'get_the_author_meta()'而不是'the_author_meta()' – 2011-06-04 09:48:17