Post Pic

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

Nice, isn't it?

Credits goes to Jeff Starr for this nice trick!

For those who have missed my great domain name deal from friday, don't worry, I got a plenty of them for sale at very good price, including facebook-developer.com, webdev.fr, etc. Just click here to visit my domain name portfolio.

Leave a Comment

* Name, Email, Comment are Required