Blogger Ever

How to get Facebook comment count for a web page using jQuery?

This post will guide you to attain the comment count for the Facebook comment system on a web-page using Graph API and jQuery.
How to get Facebook comment count for a web page using jQuery?

In my last post I'd shared in detailed post on Facebook comment box. When their is comment box then their is comment count. Every good blog shows the number of comments on each post. I'm going to share a easy-to-use method get comment counts. We are going to use jQuery's $.getJSON() function and Facebook graph API.
Like we pulled out data from Facebook using jQuery we have to do the same but the change will be that we are going to take comments count this time.

Facebook stores every comment and shares for each link in their database, if their is any comment present then the JSON path for that comment will be (taking data as argument):
data.comments
I'm not giving any pen or demo right here because its easy-as-pie, so lets go straight forward:

<span class="comment-count"></span>
<script>
$(function () {
    var url = 'http://YOUR-URL.com' // Replace this variable value with the link
    $.getJSON('http://graph.facebook.com/?id=' + url, function (data) {
        if (data.comments) { //If their is no comments on the page then comments data will not exist
            $('.comment-count').append(data.comments);
        } else {
            $('.comment-count').append('No Comments');
        }
    });
});
</script>
Blogger

2013-2016 Copyright Blogger E\ver . Coded, managed and founded by Mohammad Hamza (@hamzadhamiya) . Hosted on Blogger and fonts used are Raleway and Open Sans.