Add Twitter Trends To Your WordPress Blog

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();

2 thoughts on “Add Twitter Trends To Your WordPress Blog

  1. 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.

Comments are closed.