1

For a post I'd like to count the comment threads and not include nested replys in the count.

Currently I'm using a simple comment count, comments_number( $zero, $one, $more );

What would be the best way to get the number of comment threads in a post?

Any suggestions would be greatly appreciated.

1 Answer 1

0

You could try this, must be used in the loop:

<?php

// type = comment will only get "real" comments, no ping-/trackbacks
$comments = get_comments(array('type' => 'comment'));

$threads = 0;
foreach($comments as $comment) {
   // if the comment has no parent, it´s the first of a thread
   if($comment->parent == '') { $threads++; }
}  

switch($threads) {
   case 0: echo "There are no comments so far."; break;
   case 1: echo "There is one thread..."; break;
   default: echo "There are '.$threads.' comment threads going on."; break;
}

?>
3
  • Thanks, How would I format this complete it to just replace the existing format of? <?php comments_number( $zero, $one, $more ); ?>
    – Jon Laba
    Commented Jan 25, 2016 at 2:25
  • I updated my code. Basically, the thread count is saved in $threads, so you can use it in several different ways...
    – flomei
    Commented Jan 25, 2016 at 7:21
  • Did this work for you? If so, I´d enjoy if you mark my answer as the right solution.
    – flomei
    Commented Jan 26, 2016 at 1:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.