Comment And Ping Count In WordPress 2.7

The comment area changed a lot in WordPress 2.7. A good reason to check your comments.php and make some changes to it. Especially I like to seperate the comments from Ping- and Trackbacks. Matt shows in an excellent tutorial how to do it: Separating Pings from Comments.

But I didn't like one thing in this tutorial. To show the total count of your comments without Pings/Trackbacks, he writes following function in functions.php:

<?php
add_filter('get_comments_number', 'comment_count', 0);
function comment_count( $count ) {
	global $id;
	$comments_by_type = &separate_comments(get_comments('post_id=' . $id));
	return count($comments_by_type['comment']);
}
?>

This filter will assign globally of course. But in our blog right under the post title we have the number of comments displayed. If there is no comment but for example 2 Pingbacks, it still shows 0 comments, even though Pingbacks are kind of a comment.

The Solution

<?php
if ( ! empty($comments_by_type['comment']) ) :
    $count = count($comments_by_type['comment']);
    ($count !== 1) ? $txt = "Comments" : $txt = "Comment"; ?>
    <h3 id="comments"><?php echo $count . " " . $txt; ?></h3>

The same for Pings and everything is perfect. :)


14 Comments
  1. Jared says:

    Thanks for the Snippet ;-)
    I use a Variation for my tweetbacks.

    
    
    		
  2. Peter Zhang says:

    Really helpful. Thanks!

  3. WPCult says:

    I need to apply this to my current theme. I hacked the comments to split the comments and ping, but didn't change the counter..

  4. Michael says:

    WPCult, did you use Matt's code? Than you dont have to use his function comment_count(). Delete that function and use my code in the comments.php

  5. And if you'd like to number your comments one by one so the numbers will still work even with date order reversed, even with paging enabled, and even with threading enabled, there's a handy new plugin (shameless plug, cough splutter, ahem) which will do the trick:

    Greg's Threaded Comment Numbering

    Very handy, IMHO...

    All the best,
    Greg (yup, the plugin guy)

  6. Alex says:

    Hey Greg, thanks for your plugin, I'm sure, many people will use it.

  7. Goedkope says:

    WordPress 2.7 has introduced many new features surrounding comments. Of these is AJAX commenting and threaded comments. To take advantage of the later, you must use a function wp_list_comments instead of the old way of looping through the comments array with a foreach.

  8. Vittorio says:

    Hi Michael,
    your snippet seems exactly what I was looking for, but I can't understand where I should put your code and how I could do "the same for Pings", could you please explain it?

    Basically, what I need is only to display something like "This post has 3 comments and 2 pings" for each article: has this anything to do with the comments_popup_link function?

    Thanks,
    Vittorio

  9. Michael says:

    @Vittorio: The code has nothing to do with the comment_popup_link function. Its a part of comments.php. For the Ping part you can use

    if ( ! empty($comments_by_type['pings']) ) :
        $count = count($comments_by_type['pings']);
        ($count !== 1) ? $txt = "Pings" : $txt = "Ping"; ?>
        <h3 id="pings"><?php echo $count . " " . $txt; ?></h3>
    
  10. Vittorio says:

    Thanks Michael for your quick reply.

    I understand this code has to go in comments.php, and I added it just below the same snippet for normal comments, as you pointed in your post.

    Btw, my problem is still unsolved: how can I display, for each article in the frontpage, 2 separate counts, like "This post has X comments and Y pings"?

    Sorry to bother, and thank you again :)

  11. Pomy says:

    I want to show the total no. of comments from intense debate into footer like
    Total Comments : 56 ( or Whatever)
    how can I achieve this while I'm using the intense depate plugin..
    Note: I don't want to use intense debate widget... I want to get it manually into my template...

    Any Idea?

    Thanks,

  12. Pomy says:

    @ Michael...
    Sorry to say...you did not understand my question... I'm repeating..
    I don't want to show the wordpress own comments which made from template..but im using intense debate plugin for making comments ( intensedebate.com)..
    So, I want to show the total comments which people made from intense debate... like
    total comments from intense debate plugin = 156
    I hope you can understand what I'm asking
    Here is a screenshot for better understand..
    http://img819.imageshack.us/img819/9433/intensedebate.png

    Hope you'll guide me again..Thanks

  13. Michael says:

    @Pomy: Sorry, i don't have intense deate. Can't help ya.

Leave a Reply