Ulyssesonline

the tech surfer

  • Home
  • About
  • Archives
You are here: Home / General / Add Twitter Trends To Your WordPress Blog

July 18, 2010

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;
    }
  }
}

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>';

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

if ( function_exists( 'get_twitter_trends' ) ) get_twitter_trends();

Filed Under: General, PHP, WordPress Tagged With: api, plugin, trends, twitter, WordPress

Comments

  1. Coach says

    January 8, 2011 at 2:06 am

    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

  2. Ulysses says

    January 16, 2011 at 2:55 am

    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.

Copyright © 2003 - 2018