not working on my WordPress powered blog

2019-04-16 04:26发布

I am using the Facebook comments plugin on WordPress and the comments box is working fine but I want to access the number of counts on the index page and on single pages. On the pages, the Facebook Javascript is loaded on the pages.

Here's the code I used: <fb:comments-count href=<?php echo get_permalink() ?>/></fb:comments-count> comments

But it doesn't count the FB comments.

Is there a simple code that let me retrieve the number of comment counts?

Thanks,

6条回答
唯我独甜
2楼-- · 2019-04-16 04:56

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.

查看更多
我命由我不由天
3楼-- · 2019-04-16 05:07

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.

查看更多
混吃等死
4楼-- · 2019-04-16 05:12

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));
}
查看更多
淡お忘
5楼-- · 2019-04-16 05:18

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);

}

查看更多
我想做一个坏孩纸
6楼-- · 2019-04-16 05:20

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.

查看更多
甜甜的少女心
7楼-- · 2019-04-16 05:21

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.

查看更多
登录 后发表回答