WordPress.org

Forums

[resolved] Displaying comment and ping counts separately (2 posts)

  1. Raam Dev
    Member
    Posted 5 years ago #

    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'); ?>
            </ol>

    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?

  2. Raam Dev
    Member
    Posted 4 years ago #

    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";

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.