X

Get total Facebook likes

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

Comments  Leave a Reply

  1. Do I need to add anything else for this to work? It just outputs 0;

    1. @c74a43f8a3edc5e1429dda40cf329a8c:disqus nope that is it, however you may want to contact your hosting provider to make sure they have “file_get_contents” some shared hosting solutions do not have this enabled.

      1. Hector Castilla April 18, 2014 at 1:22 am

        Hi kevin!

        how to put ” like” or the hand icon after the like number?

        thanks

      2. Hector Castilla April 18, 2014 at 1:22 am

        Hi kevin!

        how to put ” like” or the hand icon after the like number?

        thanks

      3. Hector Castilla April 18, 2014 at 1:22 am

        Hi kevin!

        how to put ” like” or the hand icon after the like number?

        thanks

      4. Hector Castilla April 18, 2014 at 1:22 am

        Hi kevin!

        how to put ” like” or the hand icon after the like number?

        thanks

      5. Hector Castilla April 18, 2014 at 1:22 am

        Hi kevin!

        how to put ” like” or the hand icon after the like number?

        thanks

      6. Hector Castilla April 18, 2014 at 1:22 am

        Hi kevin!

        how to put ” like” or the hand icon after the like number?

        thanks

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!