The code
Place the code bellow in your theme’s functions.php file.
function commentCount($type = 'comments'){
if($type == 'comments'):
$typeSql = 'comment_type = ""';
$oneText = 'One comment';
$moreText = '% comments';
$noneText = 'No Comments';
elseif($type == 'pings'):
$typeSql = 'comment_type != ""';
$oneText = 'One pingback/trackback';
$moreText = '% pingbacks/trackbacks';
$noneText = 'No pinbacks/trackbacks';
elseif($type == 'trackbacks'):
$typeSql = 'comment_type = "trackback"';
$oneText = 'One trackback';
$moreText = '% trackbacks';
$noneText = 'No trackbacks';
elseif($type == 'pingbacks'):
$typeSql = 'comment_type = "pingback"';
$oneText = 'One pingback';
$moreText = '% pingbacks';
$noneText = 'No pingbacks';
endif;
global $wpdb;
$result = $wpdb->get_var('
SELECT
COUNT(comment_ID)
FROM
'.$wpdb->comments.'
WHERE
'.$typeSql.' AND
comment_approved="1" AND
comment_post_ID= '.get_the_ID()
);
if($result == 0):
echo str_replace('%', $result, $noneText);
elseif($result == 1):
echo str_replace('%', $result, $oneText);
elseif($result > 1):
echo str_replace('%', $result, $moreText);
endif;
}
Change the $oneText, $moreText, $noneText variable values to suit your needs. The percentage character (%) can be used in all of the variables and will be replaced with the number.
Using the code
commentCount(); //echoes the comment count
commentCount('comments'); //same as the example on top
commentCount('pings'); //echoes number of trackbacks and pingbacks
commentCount('trackbacks'); //echoes number of trackbacks
commentCount('pingbacks'); //echoes number of pingbacks
If you know a simpler way to do this let me and the readers know in the comments. Thanks.
Our WordPress themes
We have a few WordPress themes that we would like you to take a look at if you want.
check out the portfolio











I like pingbacks and trackbacks, they can provide the visitors with new stuff to read. Even internal pingbacks are a very good way to engage the reader, but mixind them up with comments is not a good behaviour. WordPress should make this by default.
In my opinion mixing them up with comments breaks the conversation. They’ll probably make it as an optional feature in some of the future versions but don’t think it will be by default. From so many wordpress users some of them might want them to be mixed. Time will show
PS I made a tutorial on separating them a month ago so this is basically an adition to that tutorial http://wpcanyon.com/tutorials/separating-trackbacks-and-pingbacks-from-comments-in-wordpress/
Hi, this tip has made my day, really. Thank you so much!!
(Most of the articles related to separating comments/trackbacks talk about comments.php and not a simple counting method and I had to spend quite some time to get here…!)
Just for your interest, I’ve used your code with permalinks wrapped around the final echo lines with the $type value as an anchor text like so;
echo '<a>'; echo str_replace('%', $result, $noneText); echo '</a>';Just to keep the index.php coding easier(lazier)… lol
thanks again!
@taqumi – Glad to help
great tutorial and it works great. Just one last problem:
How do i post it above the post, so that the displayed comments are linked..
i mean this part:
Admin Edit: use [php] CODE [/php] when posting a code
Ah, Thanks for the hint..
Here we go:
<?php comments_popup_link('0 Comments', '1 Comment', '% Comments'); ?>i want the comments_pop_link to be the correctly counted comments…
don’t know how to implement..
commentCount('comments');..here