You may notice that, over on the right hand side, next to my “Follow me on Twitter” button, there’s some text that says (currently) “2,426 others do”. If you want something similar on your WordPress blog, here’s how to do it.
First, you need to copy this code, which I picked up here, and save it into your functions.php file (via the theme editor link in the left hand column of WordPress’s admin page). You’ll need to change screen_name=XXX to replace XXX with your username.
function get_follower_count() { // first check the transient $count = get_transient('follower_count'); if ($count !== false) return $count; // no count, so go get it $count = 0; $data = wp_remote_get ('http://api.twitter.com/1/users/show.json?screen_name=XXX'); if (!is_wp_error($data)) { $value = json_decode($data['body'],true); $count = $value['followers_count']; } // set the cached value set_transient('follower_count', $count, 60*60); // 1 hour cache return $count; }
Next, you need to call the number that this function works out by adding some code at the appropriate place in your theme – the sidebar.php file in my case. One suggestion is like this:
<?php echo (get_follower_count()); ?> others do.
The problem with that is that if you’ve got more than 999 followers, there’s no formatting – so 1,000 comes out as 1000 with no comma. I know. Can you imagine?
The solution is to use the number_format command:
<?php echo number_format (get_follower_count()); ?> others do.
Now if you have 1,000 followers this will display 1,000 and not 1000. Sorted (though see comment below). (Picture credit.)
You might also like
- Google Instant filters put gay and lesbian on a par with rape, racism and paedophilia
- How to work out what Google Instant means for your business
- Google Instant keyboard navigation increases likelihood of clicking PPC ads
- Google autocomplete now fixes spelling problems
- Google puts the anal in analytics
Leave a comment!