dongtuwu8548 2011-06-04 04:49
浏览 121
已采纳

动态Twitter关注按钮

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; ?>
  • 写回答

1条回答 默认 最新

  • duanchen1937 2011-06-04 05:06
    关注

    Instead of the_author() use get_the_author(). the_author() prints the name, latter returns the name.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?