给你:
add_shortcode(\'latest_tweets\', \'latest_tweets\');
function latest_tweets($atts){
extract(shortcode_atts(array(
\'max\' => 5
), $atts));
$twitter_id = esc_attr(strip_tags($atts[0]));
// try to get data from cache to avoid slow page loading or twitter blocking
if (false === ($output = get_transient("latest_tweets_{$twitter_id}"))):
$doc = new DOMDocument();
$feed = "http://twitter.com/statuses/user_timeline/{$twitter_id}.rss";
$doc->load($feed);
$output = "<ul>";
$i = 1;
foreach ($doc->getElementsByTagName(\'item\') as $node) {
$tweet = $node->getElementsByTagName(\'title\')->item(0)->nodeValue;
//if you want to remove the userid before the tweets then uncomment the next line.
//$tweet = substr($tweet, stripos($tweet, \':\') + 1);
$tweet = preg_replace(\'@(https?://([-\\w\\.]+)+(:\\d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)?)@\', \'<a href="$1">$1</a>\', $tweet);
$tweet = preg_replace("/@([0-9a-zA-Z]+)/", "<a href=\\"http://twitter.com/$1\\">@$1</a>", $tweet);
$output .= "<li>{$tweet}</li>\\n";
if($i++ >= $max) break;
}
$output .= "</ul>\\n";
set_transient("latest_tweets_{$twitter_id}", $output, 60*10); // 10 minute cache
endif;
return $output;
}
用法:
[latest_tweets "DarthVader" max=5]
PS:您在问题中发布的代码中有一些漏洞。你应该注意这个。。。
PPS:对于想做这样事情的人:
使用Yahoo Query Language,从理论上讲,请求应该更快,并且受请求数据的服务的限制也更少
对您请求的数据使用JSON格式
使用WPHTTP API 发出外部请求