View Full Version : How to sum Wordpress and Facebook comments

08-10-2012, 05:45 PM
Hello, I would like to sum two numbers. Here is the explanation.

The following code shows the wordpress comments count:

<a href="<?php comments_link(); ?>"><?php comments_number('no comments','one comment','% comments'); ?></a>

The following code gives the Facebook comments count:

<a href="<?php comments_link(); ?>"><div class="fb-comments-count" style="display: inline" data-href="<?php echo get_permalink($post->ID); ?>">0</div> kom z FB</a>

Together the output is similar to this:

3 comments + 5 kom z FB

For reference you can see the site (http://glamourina.pl).

However, I would like this output:


It should add the two numbers and get one. How can I do? What is the right php code?

08-10-2012, 06:24 PM
I was able to do it with this function I found in internet:

function full_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;
$wpCount = get_comments_number();
$realCount = $count + $wpCount;
if ($realCount == 0 || !isset($realCount)) {
$realCount = 0;
return $realCount;

And I am putting
<?php echo full_comment_count(); ?> instead of
<?php comments_number('no comments','one comment','% comments'); ?>.

The previous code could show me the word comment for singular, plural or no comments.
The full_comment_count doesn't do it.

How can I do to make the full_comment_count choose ('no comments','one comment','% comments')?

08-10-2012, 06:31 PM
The facebook count is hard because it's javascripting.

You found this on the internet?

then try this ... of course I didn't test it, so make safe copies of your files ...

$wp_count=comments_number('no comments','one comment','% comments');
if($wp_count == "one comment"){
if($wp_count == "no comments" ){
echo "Total comments: ". $wp_count + $facebook_count;

The function from Mike Payne's site to go into function.php file:

Notice I edited it so the count is not text ...

function get_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 = 'No Comments';
} elseif ( $count == 1 ) {
//$count = '1 Comment';
} else {
//$count .= ' Comments';
echo $count;


08-10-2012, 06:40 PM
Thank you very much!!! That's it :))