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

Comments are closed.

16 comments

  1. Jared

    Thanks for the Snippet 😉
    I use a Variation for my tweetbacks.

     
    
        
    	  
      
    	  
    	
    

  2. Peter Zhang

    Really helpful. Thanks!

  3. WPCult

    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

    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. Greg Mulhauser

    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

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

  7. Goedkope

    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

    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

    @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

    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

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

    @Pomy: Take a look: http://wpengineer.com/show-amount-of-posts-pages-categories-tags-comments-for-wordpress-themes/

  13. Pomy

    @ 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

  14. Michael

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

  15. Website laten maken

    @Jared: Please explain.

  16. ARS

    Hi, I'm looking for the tricks to show comment lists numberization (not to show total comments) just like yours. so in each comment box, it will show 1, 2, 3, 4, 5, etc...

    Can you tell me the code please?

    *sorry for my bad english*