To get the comment count for each post in a WordPress query, you can use the get_comments_number() function. Here's an example of how you can achieve this inside a while loop:

<?php
// WP_Query arguments
$args = array(
    'post_type' => 'post',
    'posts_per_page' => 10,
);

// The Query
$query = new WP_Query($args);

// The Loop
if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();

        // Output post title
        echo '<h2>' . get_the_title() . '</h2>';

        // Output comment count
        $comment_count = get_comments_number();
        echo 'Comments: ' . $comment_count;

        // Rest of your post content

    }
} else {
    // no posts found
}

// Restore original post data
wp_reset_postdata();
?>

In this example, we first set up the arguments for the WP_Query to fetch the posts you want. Then, inside the while loop, you can use get_the_title() to get the post title and get_comments_number() to get the comment count for each post.

Don't forget to call wp_reset_postdata() after the loop to restore the original post data.

Note: This code assumes you are using it within a WordPress template file

i want to get comment count in wp query for each post in while

原文地址: http://www.cveoy.top/t/topic/iVOG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录