基于this answer 我建议使用以下代码:
// the theme\'s functions.php
/**
* Get the number of top level comments for a post.
*
* @param int $post_id
* @return int
*/
function wpse_95242_top_level_comments_for_post( $post_id = NULL )
{
if ( NULL === $post_id )
$post_id = get_the_ID();
if ( ! $post_id )
return 0;
add_filter( \'comments_clauses\', \'wpse_95242_where_top_comments_only\' );
$comments = get_comments(
array (
\'post_id\' => $post_id
)
);
remove_filter( \'comments_clauses\', \'wpse_95242_where_top_comments_only\' );
if ( ! $comments )
return 0;
return count( $comments );
}
/**
* Filter comment query for top level comments.
*
* @wp-hook comments_clauses
* @param array $clauses
* @return array
*/
function wpse_95242_where_top_comments_only( $clauses )
{
$clauses[\'where\'] .= \' AND comment_parent = 0\';
return $clauses;
}
在循环中,对每个项调用函数:
printf(
__( \'%d top level comments\', \'theme_text_domain\' ),
wpse_95242_top_level_comments_for_post()
);