下面是我用来获取最新推文的函数。这样做的好处是:
有关详细信息,请阅读this tutorial:
function yourprefix_latest_tweet( $username )
{
$key = "tweet-{$username}";
if ( false != ( $tweet = get_transient( $key ) ) )
return $tweet;
$args = array(
\'screen_name\' => $username,
\'count\' => 1,
\'trim_user\' => 1,
);
$url = \'https://api.twitter.com/1/statuses/user_timeline.json\';
$url = add_query_arg( $args, $url );
$request = wp_remote_get( $url );
if ( is_wp_error( $request ) )
return;
$body = wp_remote_retrieve_body( $request );
$body = json_decode( $body );
$tweet = $body[0]->text;
// Make links
$tweet = utf8_decode( $tweet );
$tweet = preg_replace(\'@(https?://([-\\w\\.]+)+(d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)?)@\', \'<a href="$1">$1</a>\', $tweet );
$tweet = preg_replace("#(^|[\\n ])@([^ \\"\\t\\n\\r< ]*)#ise", "\'\\\\1<a href=\\"http://www.twitter.com/\\\\2\\" >@\\\\2\'", $tweet);
$tweet = preg_replace("#(^|[\\n ])\\#([^ \\"\\t\\n\\r< ]*)#ise", "\'\\\\1<a href=\\"http://hashtags.org/search?query=\\\\2\\" >#\\\\2\'", $tweet);
set_transient( $key, $tweet, 3600 );
return $tweet;
}