Question:
Next I'm trying to put just a count of the shares I've had on Facebook in my theme loop . For that I found the following function and tried to modify it for my needs.
Here's 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);
?>
can anybody help me?
Answer:
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 for printing the permalink and not for fetching 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 outside the loop too.
<?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.