Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can I check if a post has Facebook comments on it and create a conditional statement? I'd like to show a small comments icon if any comments have been made.

Even better, anyone know how to get the comments count from it as well?

Thanks

share|improve this question
1  
How do you add facebook comments to your blog? –  toscho Sep 25 '12 at 19:30
add comment

2 Answers

http://developers.facebook.com/docs/reference/plugins/comments/

There's a tag you can include <fb:comments-count href=http://example.com/></fb:comments-count> which will echo the number of comments for that URL in your theme. So use something like:

<fb:comments-count href=<?php the_permalink ?>></fb:comments-count>

If you want to get the actual number without it just being echoed then you'd probably need to do some kind of client side scripting, eg with jQuery:

<span id="comment-count"><fb:comments-count href=<?php the_permalink ?>></fb:comments-count></span>

$(document).ready(function() {
    var count = $("#comment-count").html();
    if( count > 0 ) 
        $("#comment-count").html('<img src="comment-icon.png" alt="" />');
    else
         $("#comment-count").hide();
})

Above not tested in any way...

Or if you want to do it server side, which is almost certainly a better idea but will be more fiddly, then take a look at: http://developers.facebook.com/docs/reference/fql/comment/

share|improve this answer
add comment

There's a class that makes the count increment per comment. Use that within your loop with an if (!emtpy) conditional and style accordingly. See below:

The fb:comments-count tag will emit the number of comments on your URL in a <span> tag. You can then style this however appropriate to match your site. Note this is currently not available in HTML5. For example, to show the number of comments on example.com:.

<fb:comments-count href=http://example.com/></fb:comments-count> awesome comments 

Will emit:

47601 awesome comments

You can also load an iframe from Facebook which will display the number of comments with a link to your URL. For example, this shows the number of comments for example.com.

<iframe src="http://www.facebook.com/plugins/comments.php?href=example.com&permalink=1" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:130px; height:16px;" allowTransparency="true"></iframe> 

The number of comments left on a particular URL can be accessed via the Graph API. For example, the number of comments on example.com is the 'comments' field of: https://graph.facebook.com/?ids=http://example.com/.

You can also use the comment FQL table.

When you figure out the code post it here for us all! :)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.