3

I use Facebook comments on my website, and it works fine, but i can not retrieve the count of comments.

I tried the HTML5 code: <div class="fb-comments-count" data-href="http://www.bmeme.hu/?site=post&amp;id=20">0</div> but it returns with 0 instead of the real number.

I checked my page here: https://graph.facebook.com/comments/?ids=http://www.bmeme.hu/?site=post&id=20 but as you can see it returns nothing.

Why cannot get facebook comments count?

Thanks

Update: Here is my code, what I use:

<div class="fb-comments-count" data-href="<? echo urlencode($siteurl."/?site=post&id=".$value["id"]); ?>" style="display: inline;">0</div> komment

$siteurl = http://www.bmeme.hu
$value["id"] //the actual post id

And in the first line in <body> tag:

<div id="fb-root"></div>
<script>(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/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

And not works..

Tamas Kinsztler
  • 182
  • 1
  • 2
  • 14

2 Answers2

2

This is what I used to get the comments count working. (This is valid HTML 5 code, use the example on the facebook website for older templates).

//Include this in the footer of your page.  
(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_GB/all.js#xfbml=1&appId=xxxxxxxxxxxxxx";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

Now use this div to retrieve the comment count (It will display as 0 because the first time you use it, you probably wont have any comments on your page".

<div class="fb-comments-count" data-href="http://www.yourpagelinkhere.com"></div>
Anil
  • 21,730
  • 9
  • 73
  • 100
1

You're encoding URL in a wrong manner.

Attribute data-href should be http://www.bmeme.hu/?site=post&id=20 (notice & and not &amp;). Just like it defined on your Comments Social Plugin.

To get comments via OpenGraph you should properly encode whole URL with encodeURIComponent:

https://graph.facebook.com/comments/?ids=http%3A%2F%2Fwww.bmeme.hu%2F%3Fsite%3Dpost%26id%3D20

iframe version of comments-count may be accessed by URL:

http://www.facebook.com/plugins/comments.php?href=http%3A%2F%2Fwww.bmeme.hu%2F%3Fsite%3Dpost%26id%3D20&permalink=1

Update:
Since you also care about "valid" HTML you should use URL-encoded URL for page to get this as done. I've created jsFiddle snipped there you can see both ways working: http://jsfiddle.net/rFmyX/

Juicy Scripter
  • 25,778
  • 6
  • 72
  • 93
  • If I place there & instead of & then it will not be w3c valid code. But i tried, and still nothing... The graph link fine now, but i will only the count of comments, and only with HTML5. – Tamas Kinsztler Feb 20 '12 at 00:12
  • @TamásKinsztler, I've updated my code with some clarifications and provided live working example. BTW, it's better to state in your question: that you care about validity of HTML (not everybody do, and it's not automatically implied). ;) – Juicy Scripter Feb 20 '12 at 07:16
  • I see that your code is working, but i implemented the same code on my website (as you can see), and it's isn't working. If i open the google chrome debugger, i got an error too: Uncaught TypeError: Cannot read property '0' of undefined @ all.js:52. I got this error with and without urlencode too. If I write a php script to check the comments on the graph.facebook.com link, then the site will be very slow. – Tamas Kinsztler Feb 20 '12 at 20:00
  • Remove parts after hash `#` from JS-SDK URL so Facebook will no try to call `FB.init` twice since you calling it manually. – Juicy Scripter Feb 21 '12 at 04:47