Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Add Facebook Like Button in selective WordPress Posts

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Add Facebook Like Button in selective WordPress Posts

We have written about how you can add facebook like button in WordPress posts. There are hundreds of plugins that lets you add the facebook like button at the bottom of each post. But sadly, there is not a single plugin that lets you display the like button on selective WordPress posts. In this article, we will utilize WordPress Custom fields to display the facebook like button in selective WordPress posts.

First open your single.php and find the loop (a code that looks similar to this):

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

In the loop query, let’s create a call to check for a custom field “fb-like”. You will do this by replacing the above code with the one below:

<?php if (have_posts()) : while (have_posts()) : the_post();
// check for Facebook Like on Single Page
$fblike = get_post_meta($post->ID, 'fb-like', $single = true);

Now anywhere within the loop, you will need to add the following code:

<?php // if there's a Facebook Like on Single Page
if($fblike !== '') { ?>
<iframe src="<?php echo urlencode(get_permalink($post->ID)); ?>&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:60px;"></iframe>
<?php } // end if statement
// if there's not a Facebook Like on Single Page
else { echo ''; } ?>

The code above is basically checking for the custom field “fb-like” through our get_post_meta hook. If this custom field is specified, then the facebook button will be displayed in your single posts. If not, then it will not display anything on that area.

So in any post that you want to display the Facebook Like Button, simply add the custom field with a Name “fb-like” and value “true”, and you are done.

This similar Custom Fields technique can be used for many things such as displaying a digg button on selective WordPress posts and others.

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. aru says:

    this only show the like can i count the no. of likes to that post??

  2. dje0624 says:

    can you adapt this to work with any custom field already in use. I have a custom field in every post that I want people to be able to easily share on their wall. It wouldn’t be from the single.php, it would be archive and index.php. Can you shed some light on this, I can’t find any info on this at all :(

  3. FerretPost says:

    Hey, awesome. I like livefyre comments.

  4. Ezequiel says:

    Actually, there is indeed a plugin where you can add Facebook “Like” buttons selectively. Read about it here:

  5. Jamey Stegmaier says:

    Today I added the Facebook Like button to my WordPress blog, but I’d like to take it a step further and add global Like buttons to all comments on my blog (so if someone writes a really clever comment, people can Like it). Is there a plugin for that? 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 comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.