Thursday, July 15, 2010

Display the number of tweets for each page or post

Twitter is a very cool tool, especially if you’re a blogger. Using some php it is easy to display how many times a specific blog post has been tweeted. This is what you’re going to learn in this recipe.
To apply this hack, you have to make sure that the SimpleXML PHP extension is loaded. If you're using WpWebHost or HostGator, it is.

The first step is to place the following piece of code in your functions.php file:
function tweetCount($url) { 
$content = file_get_contents("http://api.tweetmeme.com/url_info?url=".$url);
$element = new SimpleXmlElement($content);
$tweets = $element->story->url_count;
echo $tweets." tweets!";
}


Once done, open your single.php file and call the function like this:
<?php tweetCount($post->permalink); ?>

No comments:

Post a Comment