Numbering your comments, pingbacks, trackbacks or all

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 
	} ?>

Comments are closed.

23 comments

  1. ThesisWorld

    That's a good solution for PHP-savvy Ninjas. Ordinary users like me use a theme with built-in comment numbering.

  2. Jauhari

    Thanks you 😉

  3. Frank

    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.

  4. Jauhari

    I got this error

    Fatal error: Call to undefined function update_comment_type_cache()

    How t fix it?

  5. Frank

    @Jauhari: i have update the syntax with my extra function vor flush cache of comments, please add this function to your theme.

  6. kvf300

    I've got this error

    Fatal error: Call to undefined function:add_filter()

    How to fix it?

    Please Thank you

    PHP 5 actived

  7. Frank

    add_filter() is a function of the core of WordPress and give us developer possibilities for hook in the core.

  8. Sajid

    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...""""

  9. Erin

    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!

  10. WpDite

    Thanks a lot. Useful tut.

  11. Ken

    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 1219

    Looking 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.

  12. Cheryl F. (The Lucky Ladybug)

    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??

  13. RemMem

    I'm lost too, Cheryl. I tried it and I received an error and couldn't access my site. I would love some directions for the non-web master. 🙂 I've tried a few different plugins to no avail.

  14. BJ

    So true with Cheryl F.

    This is WebDev talk..
    And even worse.. all sites online are just copy-/pasting the same story without knowing what they say just to gain traffic..

    Very sad there is not one site online that just tells us none web-dev's in Micky Mouse language how to get comment -counts or numbers next to each post. Nothing more nothing less we don't need fancy trackbacks and pings and all other seo stuff we just like to add something important to our comments without the use of a plugin that has 500 features + an extra stylesheet!

    Maybe this code does it all but it doesn't show you where to past it..

  15. kathy

    well guys the site IS called wpENGINEER.com and it tends to cover advanced wordpress topics. that should be a give away that the posts will be a little technical. sheesh.

    if you can't figure it out why not use a plugin such as :
    http://wordpress.org/extend/plugins/gregs-threaded-comment-numbering/

    my complaint would lie more towards the title being misleading. Unless i am missing something these functions will count the total number of comments... not number them individually the way you see at this site. i suspect that might be done in the callback fb_theme_comment, but we don't get to see it in the post..

8 pingbacks

  1. WordPress - Numerowanie komentarzy | Notatnik @ TopBlogger
  2. Come fare: numeriamo i commenti | Altamente Decorativo
  3. 30 Most Wanted WordPress Comments Page Hacks | instantShift
  4. 30 astuces pour améliorer la présentation des commentaires dans WordPress | Presse-Citron
  5. 10 WordPress Tips & Tricks for Your Comment Page | Free Tutorial 4 All
  6. 10 WordPress Tips & Tricks for Your Comment Page | OpenABlog
  7. 30个增强Wordpress留言部份的技巧 | 帕兰映像
  8. Advanced Wordpress Comment Styles and Tricks | Pro Blog Design