WordPress tutorials: Get total Facebook likes

Wpsnipp – Add the first block of PHP code to your functions.php file in your wordpress. Then add the second block of PHP code into the single.php file of your theme will display the number of Facebook likes your posts have.

 

 

function fbCount($url) {
$content = file_get_contents("http://api.ak.facebook.com/restserver.php?v=1.0&method=fql.query&query=select%20url,%20total_count%20from%20link_stat%20where%20url%20in%20('".$url."')&format=xml");
$fbCount = simplexml_load_string($content);
echo $fbCount->link_stat->total_count;
if(is_bool($fbCount)){
print '0';
}else{
echo $fbCount;
}
}

<?php
fbCount(get_permalink($post->post_id));
?>

Be Sociable, Share!

Leave a comment