Share count and facebook likes in wordpress loop

Asked

Viewed 904 times

3

Next I’m trying to put only a count of the shares I had on Facebook on loop of my theme. For this I found the following function and tried to modify it to my need.

Follows the function:

function ia_fb_count($url_post) {
//Get the transient value from database
$fbcount = get_transient( 'fblcount' );
    if ( empty( $fbcount ) ){
        //If no value stored, get the like count
       $file = file_get_contents("http://graph.facebook.com/?ids=$url_post");
            $jd = json_decode($file);
            $fbcount = number_format($jd->{'shares'});

        // Set the facebook likes transient value for 24 Hours
        set_transient('fblcount', $fbcount, 60*60*24 );
        // Also, return it at the same time
        return $fbcount;
    } else {
        // Transient Value is present, return it
        return $fbcount;
    }
}

How I call the function in my theme:

<?php
   $url_post = the_permalink(); 
   echo ia_fb_count($url_post);
?>

Could someone help me?

  • @Qmechanic73 more in my theme is not showing the Shared count but the url link.. see image: http://www.tickimg.com.br/uploads/_url_faceboo.jpg I need only the facebook Shared count to appear

  • @Qmechanic73 continues calling not only the numbering but the entire http://localhost/wordpress/? p=368851,588 .. I think this function is not catching the link going through Thema.. in $url_post .. I just don’t know how to do it and how to!

  • Doing this manually returns some result?

  • @Qmechanic73 is returning this error: http://localhost/wordpress/? p=36885 Warning: file_get_contents(http://graph.facebook.com/?ids=): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in C: xampp htdocs wordpress wp-content themes brasilseries functions.php on line 135

  • It must be because you are misformulating the url, see if that knack works.

1 answer

1

The function you are using to get the permalink is wrong. This function returns the link with the html tag: <a href="<?php the_permalink(); ?>">permalink</a>. It is to print the permalink and not to search the URL. In your code you must use the function get_permalink.

As you are called this function by theme, it is important to fetch the post ID. So you can call it off the loop as well.

<?php
   global $post;
   $url_post = get_permalink($post->ID); 
   echo ia_fb_count($url_post);
?>

Finally, if you are testing on a localhost the Graph api will return an error because it cannot find the URL.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.