<fb:comments-count> not working on my WordPress powered blog

戏子无情 提交于 2019-12-04 18:34:22
ifennec

Include this function somewhere in your template file :

function fb_comment_count() {

    global $post;
    $url = get_permalink($post->ID);

    $filecontent = file_get_contents('https://graph.facebook.com/?ids=' . $url);
    $json = json_decode($filecontent);
    $count = $json->$url->comments;
    if ($count == 0 || !isset($count)) {
        $count = 0;
    }
    echo $count;
}

use it like this in your homepage or wherever

<a href="<?php the_permalink() ?>"><?php fb_comment_count() ?></a>

Had the same problem, that function worked for me... if you get an error... try reading this.

Créateur de site

The comments often don't appear here :

graph.facebook.com/?ids = [your url]

Instead they appear well in

graph.facebook.com/comments/?ids = [your url]

Hence the value of the final solution.

Answer by ifennec seems fine, but actually is not working (facebook maybe changed something and now is only returning the number of shares).

You could try to get all the comments:

    $filecontent = file_get_contents(
        'https://graph.facebook.com/comments/?ids=' . $url);

And count all:

    $json = json_decode($filecontent);
    $content = $json->$url;
    $count = count($content->data);

    if (!isset($count) || $count == 0) {
       $count = 0;
    }
    echo $count;

This is just a fix until facebook decides to read the FAQ about fb:comments-count, and discovers it's not working :) (http://developers.facebook.com/docs/reference/plugins/comments/ yeah, awesome comments).

By the way, I applied the function in Drupal 7 :) Thank you very much ifennec, you showed me the way.

This works for me :

function fb_comment_count() {
  global $post;
  $url = get_permalink($post->ID);
  $filecontent = file_get_contents('https://graph.facebook.com/comments/?ids=' . $url);
  $json = json_decode($filecontent);
  echo(count($json->$url->comments->data));
}

This is resolved.

<p><span class="cmt"><fb:comments-count href=<?php the_permalink(); ?>></fb:comments-count></span> Comments</p>

The problem was that I was using 'url' than a 'href' attribute in my case.

Just put this function in functions.php and pass the post url to function fb_comment_count wherever you call it on your theme files

function fb_comment_count($url) {
$filecontent    = file_get_contents('https://graph.facebook.com/comments/?ids=' . $url);
$json           = json_decode($filecontent);
$content        = $json->$url;

echo count($content->comments->data);

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!