Numbering your comments, pingbacks, trackbacks or all
February 3rd, 2009 by Frank • WordPress Hacks • 20 Comments
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() ) { ?>
<?php if ( empty($comments_by_type['comment']) ) { ?>
<h2 id="comments"><?php fb_comment_type_count( 'comment' ); ?></h2>
<ol class="commentlist">
<?php wp_list_comments( 'type=comment&callback=fb_theme_comment' ); ?>
</ol>
<?php } ?>
<?php
if ( function_exists( 'fb_comment_type_count' ) ) {
// alternative type pings for trackback + pingback
if ( empty($comments_by_type['$post_id']) ) { ?>
<h2 id="pingback"><?php fb_comment_type_count( 'pingback', 'No Pingback', 'OnePingback', '% Pingbacks' ); ?></h2>
<ol class="pingbacklist">
<?php wp_list_comments('type=pingback'); ?>
</ol>
<?php } ?>
<?php
// alternative type pings for trackback + pingback
if ( empty($comments_by_type['trackback']) ) { ?>
<h2 id="trackback"><?php fb_comment_type_count( 'trackback', 'No Trackback', 'One Trackback', '% Trackbacks' ); ?></h2>
<ol class="trackbacklist">
<?php wp_list_comments('type=trackback'); ?>
</ol>
<?php }
} ?>
<div class="navigation nav_comments">
<div class="alignleft"><?php previous_comments_link() ?></div>
<div class="alignright"><?php next_comments_link() ?></div>
</div>
<?php } else {
// this is displayed if there are no comments so far
} ?>Info
- Published in WordPress Hacks
- Tags: Code, comments, Theme, WordPress, WordPress Themes, WordPress Tutorials, WP, wp2.7
- Comment feed | Trackback URL
- read: 16904 | today: 9
- leave a Comment
12 Comments
8 Pings
- WordPress - Numerowanie komentarzy | Notatnik @ TopBlogger
- Come fare: numeriamo i commenti | Altamente Decorativo
- 30 Most Wanted WordPress Comments Page Hacks | instantShift
- 30 astuces pour améliorer la présentation des commentaires dans WordPress | Presse-Citron
- 10 WordPress Tips & Tricks for Your Comment Page | Free Tutorial 4 All
- 10 WordPress Tips & Tricks for Your Comment Page | OpenABlog
- 30个增强Wordpress留言部份的技巧 | 帕兰映像
- Advanced Wordpress Comment Styles and Tricks | Pro Blog Design



That's a good solution for PHP-savvy Ninjas. Ordinary users like me use a theme with built-in comment numbering.
Thanks you
PLEASE, update the functions! I have update the code for more possibilities with the words of No (One) (Count) pingsback(s), No (One) (Count) trackback(s) etc.
I hope you enjoy this.
I got this error
Fatal error: Call to undefined function update_comment_type_cache()
How t fix it?
@Jauhari: i have update the syntax with my extra function vor flush cache of comments, please add this function to your theme.
I've got this error
Fatal error: Call to undefined function:add_filter()
How to fix it?
Please Thank you
PHP 5 actived
add_filter()is a function of the core of WordPress and give us developer possibilities for hook in the core.The plug-ins is easier to install now.
I think I got some result. Thank you here.
"""But at some points like above comments show there is problem in this...""""
Thanks a lot for this. I'd already separated pingbacks from comments on a site, but was having trouble getting the pingback count to work. -was getting pretty frustrated. Thanks for saving the day!
Thanks a lot. Useful tut.
Thanks for teaching me what the functions.php theme file does!
I'm sure I'm coming into the discussion out-of-context, I was trying to patch the code into my existing template. The most perplexing issue was:
Warning: call_user_func(fb_theme_comment) [function.call-user-func]: First argument is expected to be a valid callback in [snipped] /wp-includes/comment-template.php on line 1219Looking for the solution landed me on the wp-basis-theme page, and I was able to snip out the needed function from there.
Thanks again.
I would love to number my comments, but this is way too hard to understand. Definitely written from a developer's viewpoint. It's hard to tell where to place code. I give up after coming up with several different errors. Isn't there an easier way??