Share and tanned facebook count in the wordpress loop

3

Next I'm trying to put just one share count I've had on Facebook in the loop of my theme. For this I found the following function and tried to modify it for my need.

Follow 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 do I call the function in my theme:

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

Could anyone help me?

    
asked by anonymous 09.03.2015 / 14:46

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 fetch the URL. In your code you should use the get_permalink function.

Since you're calling this function by theme, it's important to get the post ID. So you can call it out of 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 can not find the URL.

    
11.03.2015 / 20:41