Support » Themes and Templates » Displaying comment and ping counts separately

  • Resolved Raam Dev


    I’m trying to display the comment count above my comment list in comments.php without the pings included. The comments_number() function doesn’t allow for displaying them separately (why?), so I need to access the $wp_query object directly.

    Several examples on the web show this being done like this:

    <?php if ( have_comments() ) : global $wp_query;  ?>
            <h3 id="comments"><?php echo count($wp_query->comments_by_type['comment']); ?> comments</h3>
            <ol class="commentlist">
                    <?php wp_list_comments('type=comment&callback=cleanr_theme_comment'); ?>

    However, this doesn’t work.

    After experimenting, I discovered that I’m able to access the $wp_query object after calling wp_list_comments() (looking at the code, that function declares $wp_query as global).

    How can I access the $wp_query object before calling wp_list_comments(), so that I can gain access to the comment count without pings included?

Viewing 1 replies (of 1 total)
  • For anyone who finds this post and is looking for a solution, WordPress 3.1 modified the get_comments() function to allow for specifying the comment type.

    This means if you’re trying to display comment, trackback, and pingback counts separately, you can do so like this (must be placed within The Loop):

    $post_comments = get_comments(array('type' => 'comment','post_id' => $post->ID));
    $post_trackbacks = get_comments(array('type' => 'trackback','post_id' => $post->ID));
    $post_pingbacks = get_comments(array('type' => 'pingback','post_id' => $post->ID));
    echo count($post_comments) . " Comments";
    echo count($post_trackbacks) . " Trackbacks";
    echo count($post_pingbacks) . " Pingbacks";
Viewing 1 replies (of 1 total)
  • The topic ‘Displaying comment and ping counts separately’ is closed to new replies.