Are you looking for a way to get total number of Facebook likes your posts have? While there’s probably a plugin for this, we have created a quick code snippet that you can use to get total Facebook likes and display them in WordPress.
Instructions:
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
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; } }
Add this code to your theme’s single.php file to display the total number of Facebook likes your posts have.
<?php fbCount(get_permalink($post->post_id)); ?>
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 27 best WordPress business themes for your website and how to create a WordPress donation form.
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.
Hi kevin!
how to put ” like” or the hand icon after the like number?
thanks
Hi kevin!
how to put ” like” or the hand icon after the like number?
thanks
Hi kevin!
how to put ” like” or the hand icon after the like number?
thanks
Hi kevin!
how to put ” like” or the hand icon after the like number?
thanks
Hi kevin!
how to put ” like” or the hand icon after the like number?
thanks
Hi kevin!
how to put ” like” or the hand icon after the like number?
thanks