...

View Full Version : Wordpress not summing well FB and WP comments



utnalove
08-17-2012, 10:32 PM
Hi, I am using the following code:


<a href="<?php comments_link(); ?>"><?php
$commentCount = full_comment_count();
if ( $commentCount == 0 ) {
echo 'add a comment';
}
else if ( $commentCount == 1 ) {
echo 'comment';
}
else {
echo '' . $commentCount . ' comments';
}
?></a>

and this code:

// Get combined FB and WordPress comment count
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;
}

Problem explained:
If there are 2 FB comments and 3 WP the output is: 5 comments
If there are no comments the output is:add a comment
If there are 0 FB comments and 1 WP the output is: comment

In the last example is missing the number 1.
Anybody can find the error in the code?

Lamped
08-18-2012, 01:20 AM
<a href="<?php comments_link(); ?>"><?php
$commentCount = full_comment_count();
if ( $commentCount == 0 ) {
echo 'add a comment';
}
else if ( $commentCount == 1 ) {
echo '1 comment';
}
else {
echo '' . $commentCount . ' comments';
}
?></a>

or...


<a href="<?php comments_link(); ?>"><?php
$commentCount = full_comment_count();
if (!$commentCount) {
echo 'add a comment';
} elseif ($commentCount == 1) {
echo '1 comment';
} else {
echo $commentCount . ' comments';
}
?></a>

or ...


<a href="<?php comments_link(); ?>"><?php
$commentCount = full_comment_count();
if ($commentCount) {
echo $commentCount . ' comment' . ($commentCount > 1 ? 's' : '');
else {
echo 'add a comment';
}
?></a>

utnalove
08-18-2012, 10:33 AM
wow 3 solutions! Thanks so much!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum