Since WordPress 2.7 exists an API for the comment area. Thereby you can purge the PHP-portion within comments.php
. Jean-Baptiste shows a simple solution to count comments. This solution is very well known and get used pretty much since the first release of WordPress. But if you like to have a filter, which counts only the trackbacks or only the comments, then this solutions is worthless. Besides that, the How To is pretty poor and PHP-rookies could have troubles with it.
Relating to this and because of many questions about this topic I show you two simple functions, which you insert in your functions.php
of your theme. Normally they get used as output in your comments.php
.
/**
* count for Trackback, pingback, comment, pings
*
* use it:
* fb_comment_type_count('ping');
* fb_comment_type_count('comment');
*/
if ( !function_exists('fb_comment_type_count') ) {
function fb_get_comment_type_count( $type='all', $zero = false, $one = false, $more = false, $post_id = 0) {
global $cjd_comment_count_cache, $id, $post;
if ( !$post_id )
$post_id = $post->ID;
if ( !$post_id )
return;
if ( !isset($cjd_comment_count_cache[$post_id]) ) {
$p = get_post($post_id);
$p = array($p);
update_comment_type_cache($p);
}
;
if ( $type == 'pingback' || $type == 'trackback' || $type == 'comment' )
$count = $cjd_comment_count_cache[$post_id][$type];
elseif ( $type == 'pings' )
$count = $cjd_comment_count_cache[$post_id]['pingback'] + $cjd_comment_count_cache[$post_id]['trackback'];
else
$count = array_sum((array) $cjd_comment_count_cache[$post_id]);
return apply_filters('fb_get_comment_type_count', $count);
}
// comment, trackback, pingback, pings, all
function fb_comment_type_count( $type='all', $zero = false, $one = false, $more = false, $post_id = 0 ) {
$number = fb_get_comment_type_count( $type, $zero, $one, $more, $post_id );
if ( $number > 1 )
$output = str_replace('%', number_format_i18n($number), ( false === $more ) ? __('% Comments') : $more);
elseif ( $number == 0 )
$output = ( false === $zero ) ? __('No Comments') : $zero;
else // must be one
$output = ( false === $one ) ? __('1 Comment') : $one;
echo apply_filters('fb_comment_type_count', $output, $number);
}
}
if ( !function_exists('fb_update_comment_type_cache') ) {
function fb_update_comment_type_cache(&$queried_posts) {
global $cjd_comment_count_cache, $wpdb;
if ( !$queried_posts )
return $queried_posts;
foreach ( (array) $queried_posts as $post )
if ( !isset($cjd_comment_count_cache[$post->ID]) )
$post_id_list[] = $post->ID;
if ( $post_id_list ) {
$post_id_list = implode(',', $post_id_list);
foreach ( array('', 'pingback', 'trackback') as $type ) {
$counts = $wpdb->get_results("SELECT ID, COUNT( comment_ID ) AS ccount
FROM $wpdb->posts
LEFT JOIN $wpdb->comments ON ( comment_post_ID = ID AND comment_approved = '1' AND comment_type='$type' )
WHERE post_status = 'publish' AND ID IN ($post_id_list)
GROUP BY ID");
if ( $counts ) {
if ( '' == $type )
$type = 'comment';
foreach ( $counts as $count )
$cjd_comment_count_cache[$count->ID][$type] = $count->ccount;
}
}
}
return $queried_posts;
}
add_filter('the_posts', 'fb_update_comment_type_cache');
}
If both functions in your theme are available, the usage of the 3 forms comments, trackbacks and pingbacks could look like this:
if ( function_exists('wp_list_comments') ) {
// WP 2.7 comment loop
if ( have_comments() ) { ?>