公式ウィジェットで十分綺麗なので良いんですが
なんとなく自分で実装したいと思って調べてみたところあくちーさん( @actywav )のサイトを見つけました!!
→WordPress のプラグイン無しですごく簡単にツイート表示しよー : actywayふむふむ。やることは大まかにこの3つの様です。
- functions.phpに処理実装
- CSS調整
- jquery.totemtickerの設置
:
なるほど!キャッシュですか!先生!
ということでTransients APIについては
全く知らなかったので、これを使う方向でカスタマイズしようと思ったのですが・・・どうやら今回使用しているsimplexml_load_file関数と、Transients APIは
相性が悪いらしく、一旦実装を断念しました・・・。
※調べた所、simplexml_load_file関数はシリアライズ処理が上手くいかないのだそうです。
via. Twitterのツイートを表示する-RSS編 | Web-Clutch
ってわけで、Transients API に対応させてみた。未検証。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function get_tweet_lines($userName){ | |
date_default_timezone_set('Asia/Tokyo'); | |
$rssUrl = 'http://twitter.com/statuses/user_timeline/'.$userName.'.rss'; | |
// get cache | |
if ( ($response = get_transient(md5($rssUrl))) === false ) { | |
$response = wp_remote_get($rssUrl); | |
if( !is_wp_error( $response ) && $response["response"]["code"] === 200 ) { | |
set_transient(md5($rssUrl), $response, 60 * 60 ); // 60sec * 60min = 1hour | |
} else { | |
$response = false; | |
} | |
} | |
if( $response && !is_wp_error($response) ) { | |
$rssData = simplexml_load_string($response["body"]); | |
echo '<ul id="vertical-ticker" class="tweet-widget">'; | |
foreach($rssData->channel->item as $item){ | |
$pubDateJP = date("Y/m/d H:i:s",strtotime($item->pubDate)); | |
echo "<li>".str_replace("\n",'',$item->description)."<p><a href='".$item->link."'>".$pubDateJP."</a> のつぶやき。</p></li>\n"; | |
} | |
echo '</ul>'; | |
} else { | |
// Handle error here. | |
} | |
} |
コメントを残す