
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.

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.
11 Responses
This is really helpful. Bloggers should realize the power of social media and use it to promote their web presence.
Well…the fact that you refer to this technique as a “hack” makes me unlikely to use it.
@David Zemens : There’s no obligation to use any of the hacks/tips/codes you can found on that site, so no problem
Very Useful info…thanks for sharing
Cool, but I stopped using tweetmeme on some sites, wonder how to get that done with backtype.
@David Zemens: It sounds like you’re operating off the uninformed public’s definition of “hack.” The real definition of a hack, in this case, is: A way of doing something that involves adding quick-and-dirty bit of code to make a piece of software do what you want, rather than using a prepackaged (and possibly more elegant) solution like a plugin. There’s nothing unreliable (and certainly not illegal!) about it.
Hi,
Useful information, its work fine,
thanks for nice sharing.
Wouldn’t adding some error handling be good measure? What if the API is down? Just curious to see what the end-visitor would be presented with?
Trackbacks: