Home / social / Get total Facebook likes
  • How to Get total Facebook likes

    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.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    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;
            }
    }
    
    ( click code to copy )

    wordpress snippet : PHP

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

    ( WordPress codex functions, hooks, in this snippet. )

    query, get_permalink,
  • Mathias

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

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

      • Hector Castilla

        Hi kevin!

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

        thanks

      • Hector Castilla

        Hi kevin!

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

        thanks

      • Hector Castilla

        Hi kevin!

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

        thanks

      • Hector Castilla

        Hi kevin!

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

        thanks

      • Hector Castilla

        Hi kevin!

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

        thanks

      • Hector Castilla

        Hi kevin!

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

        thanks