2011-06-04 69 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