Displaying The Facebook Page Likes In WordPress

If you have a Facebook page for your website, and you want to display the likes for the same on your WordPress website, then you need to install some plugin to do so (until and unless you know who to retrieve the likes for pages from Facebook). But this article will teach you how you can do that without installing any plugin. You need to add a few lines of code to one of your themes PHP file and you are all set.

Facebook Page Likes Count In WordPress

  1. First off, you need to know how you can edit your template files, I have an article on editing the functions.php file, which will give you an idea on editing others.
  2. Then you must decide the place where you want to show the Facebook likes. If you want to display it on single post pages then you need to paste the code in single.php or if you want to show that in the header, then you are better off editing the header.php file.
  3. Below is the code that you need to copy and paste in your theme’s template file. Make sure you change the string in the red with your Facebook page’s ID, and the string in the blue with the current Facebook likes for your page. The current page likes will be shown if the code fails to find the current likes for your page. Make sure you update this value whenever you can.

    <?php     $page_id = "PAGE-ID";     $xml = @simplexml_load_file("http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=".$page_id."") or die ("99,999");     $fans = $xml->page->fan_count;     echo $fans . ' Likes'; ?>

That’s it. In this way you can easily show off the likes for your Facebook page to get more followers.

Related Post