How to Add the Facebook Share Outside Loop in WordPress

Snippets by IsItWP

Are you looking for a way to display Facebook share link outside loop? While there’s probably a plugin for this, we have created a quick code snippet that you can use to show Facebook share outside loop in WordPress.

You need to make sure that your WordPress theme has page.php and single.php files. If these files aren’t there, then the code snippet won’t work. It will add a text link for Facebook share.


All you have to do is add this code to your theme’s sidebar.php file:

/*  you need separate single.php in your theme to work this if statement  */
if (is_single()) {
    /* This code is to show Facebook share link outside the wordpress loop */
    echo "<a href='http://www.facebook.com/sharer.php?u=";
    echo get_permalink($post->ID); // generates post url
    echo "&t=";
    echo the_title($post->title); // generates post title
    echo "' target='_blank' rel='nofollow'> Share on Facebook</a>";
    /* This is to show total number shares  */
    $url    = get_permalink($post->ID);
    $shares = json_decode(file_get_contents("http://api.facebook.com/method/fql.query?query=select%20share_count%20from%20link_stat%20where%20url='$url'&format=json"));
    echo $shares[0]->share_count;
/*  you need separate page.php to work this condition */
elseif (is_page()) {
    echo "<a href='http://www.facebook.com/sharer.php?u=";
    echo get_permalink($page->ID); // generate page url
    echo "&t=";
    echo the_title($page->title); // generate page title
    echo "' target='_blank' rel='nofollow'> Share on Facebook</a>";
    /* This is to show total number shares for page */
    $url    = get_permalink($page->ID);
    $shares = json_decode(file_get_contents("http://api.facebook.com/method/fql.query?query=select%20share_count%20from%20link_stat%20where%20url='$url'&format=json"));
    echo " - " . $shares[0]->share_count . " shares ";
} else {
    /* default Facebook share link or nothing  */

<?php if (is_single()) { ?>
<a href="http://www.facebook.com/sharer.php?u=<?php the_permalink($post->ID); ?>&t=<?php the_title($post->title); ?>" title="Share on Facebook" target="blank">Share on Facebook</a> -
<?php } ?>

You can also use this snippet by Filip Stefansson to show Facebook share link into the WordPress loop.

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 set up download tracking in WordPress with Google Analytics.

Comments  Leave a Reply

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!