2
I created a function in PHP that added up the number of comments made on facebook and wordpress to show in each post, but I think because of the updates of the plugins facebook, the code no longer works.
I tried to create a new app on facebook to use the most current version, but I did not succeed and I have no idea if I did something wrong or did everything wrong.
The code is currently like this, only facebook counter does not work, but wordpress yes:
function total_number_comment() {
global $post;
$url = get_permalink($post->ID);
$filecontent = file_get_contents('https://graph.facebook.com/?ids='.$url);
$json = json_decode($filecontent);
$fb_count = $json->$url->comments;
if ($fb_count == 0 || !isset($fb_count)) {
$fb_count = 0;
}
$wp_count = (int)get_comments_number();
echo $fb_count + $wp_count;
}
And this is the script:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'ID',
xfbml : true,
version : 'v2.4'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
Updating
I searched for more information about my mistake and did some tests again and found that it is still working, just not as before. By directly printing the "$filecontent" variable, I get the following result:
{
"URL da postagem": {
"og_object": {
"id": "id",
"description": "Descrição da postagem",
"title": "Título da Postagem",
"type": "Tipo da da Postagem",
"updated_time": "Data da atualização da Postagem"
},
"share": {
"comment_count": 6,
"share_count": 37
},
"id": "URL da Postagem"
}
}
Well, there was error in the $json variable, so I removed it and now I have this result. The point now is I don’t know how to get to the part I want, which is the "comment_count".
Could you give me a hand, please?
Is there an error? What is the value of the "filecontent" variable? Edit your question and add relevant information.
– Filipe Moraes
@Filipemoraes Sorry for the lack of information, I will improve now.
– Evans
But you need to use the function
json_decode
to convert the string contained in$filecontent
an object. After that, you can access the propertycomment_count
.– Filipe Moraes