【问题标题】:Dynamic Twitter Follow button动态 Twitter 关注按钮
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 感谢 dpacmittal!我忘了 get_the_author()
    • @tony 如果它是正确的,请务必接受 dpacmittal 的回答,如果它特别有帮助,请点赞。
    • @Tony 对于更新部分,你又做错了。使用get_the_author_meta() 而不是the_author_meta()
    猜你喜欢
    • 2013-01-25
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 2012-06-08
    • 2011-09-07
    • 2016-02-14
    • 2020-02-01
    相关资源
    最近更新 更多