The global $post variable is not set here. How do I set it?
function fb_comment_count($link = 'link') {
global $post;
$url = 'https://graph.facebook.com/';
$posturl = get_permalink($post->ID);
$url .= $posturl;
$filecontent = wp_remote_retrieve_body(wp_remote_get($url, array('sslverify'=>false)));
$json = json_decode($filecontent);
$count = $json->comments;
if ($count == 0 || !isset($count)) {
$count = 0;
}
$comments = $count;
if ($count == 1) {
$comments .= '';
}
elseif ($count == 0) {
$comments = '0';
}
elseif ($count > 1) {
$comments .= '';
}
if ($link == 'nolink') {
return $comments;
}
else {
return '<a href="'.$posturl.'#comments" title="Comments for '.$post->post_title.'">'.$comments.'</a>';
}
}
You “set” it the same way you “set” a regular (non-global) variable, with the assignment operator – an equals character (=).
http://php.net/manual/en/language.operators.assignment.php
You can also use a $GLOBALS[] array. Please see the documentation for what the global keyword actually does:
http://php.net/manual/en/language.variables.scope.php