Getting Facebook comment count within WordPress

08 Jun 2013

Getting Facebook comment count within WordPress

I’ve been working with the Facebook Comment integration into WordPress lately. Mostly because it can help cut down on spam, but also because of the grammer checker and social media benefit. I couldn’t really find a good solution online for pulling the number of comments on any given post, so here is what I ended up with. I included this in my functions.php file, and just put this: <?php get_fb_comment_count(); ?> in my single.php file where I want the comment number to be displayed.

function get_fb_comment_count() {
  global $post;
  $url = get_permalink($post-&gt;ID);
  $filecontent = file_get_contents('' . $url);
  $json = json_decode($filecontent);
  $count = $json-&gt;$url-&gt;comments;
  if ($count == 0 || !isset($count)) {
    $count = 'No Comments';
  } elseif ( $count == 1 ) {
    $count = '1 Comment';
  } else {
    $count .= ' Comments';
  echo $count;

Leave a Reply

Your email address will not be published. Required fields are marked *