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() ) { ?>
		
		
			


Posted

in

by

Comments

23 responses to “Numbering your comments, pingbacks, trackbacks or all”

  1. ThesisWorld Avatar

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

  2. […] kanał RSS notatnika TopBlogger lub zapisz się na codzienne powiadomienia mailowe.Wp-Engineer przedstawia kompletny kod, który wystarczy dodać do functions.php, by miec ponumerowane komentarze (z podziałem na […]

  3. […] puo’ dare un’ occhiata allo spelendido articolo su WpEngineer.com o provare con il plugin gregs-threaded-comment-numbering. Articolo originariamente pubblicato su […]

  4. Jauhari Avatar

    Thanks you 😉

  5. Frank Avatar

    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.

  6. Jauhari Avatar

    I got this error

    Fatal error: Call to undefined function update_comment_type_cache()

    How t fix it?

  7. Frank Avatar

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

  8. kvf300 Avatar

    I’ve got this error

    Fatal error: Call to undefined function:add_filter()

    How to fix it?

    Please Thank you

    PHP 5 actived

  9. Frank Avatar

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

  10. Sajid Avatar

    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…””””

  11. Erin Avatar

    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!

  12. WpDite Avatar

    Thanks a lot. Useful tut.

  13. […] 6) Numbering Your WordPress Comments […]

  14. Ken Avatar

    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.

  15. Cheryl F. (The Lucky Ladybug) Avatar

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

  16. […] Numbering Your Comments, Pingbacks, Trackbacks, or All […]

  17. RemMem Avatar

    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.

  18. BJ Avatar
    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..

  19. kathy Avatar
    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..