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.

11 Responses

Nov 16 2009 15:19

This is really helpful. Bloggers should realize the power of social media and use it to promote their web presence.

Nov 16 2009 16:03

Well…the fact that you refer to this technique as a “hack” makes me unlikely to use it.

Nov 16 2009 16:43

@David Zemens : There’s no obligation to use any of the hacks/tips/codes you can found on that site, so no problem ;)

Nov 16 2009 21:39

Very Useful info…thanks for sharing

Nov 16 2009 21:51

Cool, but I stopped using tweetmeme on some sites, wonder how to get that done with backtype.

Nov 16 2009 22:00

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

Nov 18 2009 07:46

Hi,

Useful information, its work fine,

thanks for nice sharing.

Nov 20 2009 20:52

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:

Leave a Comment

* Name, Email, Comment are Required