Twitterのフォロー数を表示させたいんだ!【WPハック】
Twitter大人気ですね。Twitterでマーケティングとか利用したりして、企業アカウントや芸能人アカウントもスゴック増えました。
人気のアカウントになると、フォロアー数とかもすごい数みたいです。一応Kruz-GraphixもTwitterで垂れ流しているので、フォローミー!
フォロアー数ってのも、一つの人気バロメーターみたいなので、Wordpressにフォロアー数を表示させるハックを紹介します(プラグインもあるかもしれませんがね)。

Follow Me!
このハックはTwitterAPIのUsers/Showメゾッドを利用します。詳しい事は小難しい感じなので、すっ飛ばします(Kruzもあんまり分かっていないみたいですw)
とりあえず、一旦心を鎮めてお使いのテーマのfunctions.phpを開いてください。んで下記コードを記述。
function rarst_twitter_user( $username, $field, $display = false ) {
$interval = 3600;
$cache = get_option('rarst_twitter_user');
$url = 'http://api.twitter.com/1/users/show.json?screen_name='.urlencode($username);
if ( false == $cache )
$cache = array();
// if first time request add placeholder and force update
if ( !isset( $cache[$username][$field] ) ) {
$cache[$username][$field] = NULL;
$cache[$username]['lastcheck'] = 0;
}
// if outdated
if( $cache[$username]['lastcheck'] < (time()-$interval) ) {
// holds decoded JSON data in memory
static $memorycache;
if ( isset($memorycache[$username]) ) {
$data = $memorycache[$username];
}
else {
$result = wp_remote_retrieve_body(wp_remote_request($url));
$data = json_decode( $result );
if ( is_object($data) )
$memorycache[$username] = $data;
}
if ( is_object($data) ) {
// update all fields, known to be requested
foreach ($cache[$username] as $key => $value)
if( isset($data->$key) )
$cache[$username][$key] = $data->$key;
$cache[$username]['lastcheck'] = time();
}
else {
$cache[$username]['lastcheck'] = time()+60;
}
update_option( 'rarst_twitter_user', $cache );
}
if ( false != $display )
echo $cache[$username][$field];
return $cache[$username][$field];
}
あとは、テーマフィルのお好きな所にコードをペーストしてください。”User_name”の所にtwitterNameを書けばOKです。
<?php echo rarst_twitter_user('User_name', 'name').' has '.
rarst_twitter_user('User_name', 'followers_count').' followers after '.
rarst_twitter_user('User_name', 'statuses_count').' updates.'; ?>
素で表示したら、こんな感じです(あれ?ちょっとレイアウト崩れてるしw)
関連する記事
以下の記事が関連がありそうです。ブログにtwitterのフォロー数を表示いたい【WPハック】
記事を書いたら勝手にTweetして欲しいなぁ「WordTwit」
TwitterBOTを簡単に作ってしまいたい「キャラボット」
Facebook comments:


