here’s a quick and dirty method…
in functions php, we create a callback function for wp_list_comments() which will count our comments:
$GLOBALS['parent_comments_count'] = 0;
$GLOBALS['child_comments_count'] = 0;
function count_comments( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
if($comment->comment_parent==0):
$GLOBALS['parent_comments_count']++;
else:
$GLOBALS['child_comments_count']++;
endif;
}
then, in our template where it calls wp_list_comments(), we add another with a callback to our count function, then immediately after we can echo our count. then we have the regular wp_list_comments(), which will actually output the comments:
wp_list_comments( array( 'callback' => 'count_comments' ) );
echo "parents: ". $GLOBALS['parent_comments_count'];
echo "children: ". $GLOBALS['child_comments_count'];
wp_list_comments();
emphasis on ‘dirty’, I’m sure there’s a cleaner way to do this.
Here’s another method which uses an additional query. This will get numbers regardless of pagination. We use the comment_count
variable in $post
to subtract number of parents from to get number of children.
In functions.php:
function c_parent_comment_counter($id){
global $wpdb;
$query = "SELECT COUNT(comment_post_id) AS count FROM $wpdb->comments WHERE `comment_approved` = 1 AND `comment_post_ID` = $id AND `comment_parent` = 0";
$parents = $wpdb->get_row($query);
return $parents->count;
}
In the template:
<?php
$number_of_parents = c_parent_comment_counter($post->ID);
$number_of_children = $post->comment_count - $number_of_parents;
echo "parents: ".$number_of_parents;
echo "children: ".$number_of_children;
?>