Several months ago, I wrote a post on how to retrieve Twitter trends. Today, I’ll show you how to add and display Twitter Trends in your WordPress theme. First, the code to retrieve Twitter trends.
function get_twitter_trends() { $contents = @file_get_contents("http://search.twitter.com/trends.json"); if (strpos($http_response_header[0], "200")) { $json = json_decode($contents); foreach ($json->trends as $trend) { echo $trend->name; } } } |
Add this function in your theme’s functions.php file. If you want to make the trends clickable, then use the Twitter search link. This will send users to the Twitter Search page when they click on a trend.
echo '<a href="http://twitter.com/#search?q='.$trend->name.'">'.$trend->name.'</a>'; |
Finally, place this code in your WordPress theme where you deem appropriate.
if ( function_exists( 'get_twitter_trends' ) ) get_twitter_trends(); |
Be a bit careful with this as the twitter api limits your calls per hour, so if its a high traffic blog…could be a problem. Theres a way to do it with just php/ajax too at http://www.wikidefs.com/calebbron/10TrendsTutorial.html
Coach, Thanks for sharing your work. Yes, I’m aware of the API limits. I’ve hit the limit several times. I’ve done something similar using straight PHP and MySQL, but not as elaborate as yours using Ajax.