admin管理员组

文章数量:1122832

I have a WP_Query on my homepage template that works just fine for get_the_title() and MANY other functions for each post in the loop.

However, any way of getting the comment count just grabs the real count for the first post and then repeats the same output for the rest of the posts.

I've tried both echo get_comments_number($post->ID); and comments_number();

The kicker: if I use the same template NOT on the homepage, comment count works fine.

I have a WP_Query on my homepage template that works just fine for get_the_title() and MANY other functions for each post in the loop.

However, any way of getting the comment count just grabs the real count for the first post and then repeats the same output for the rest of the posts.

I've tried both echo get_comments_number($post->ID); and comments_number();

The kicker: if I use the same template NOT on the homepage, comment count works fine.

Share Improve this question asked Nov 24, 2014 at 2:28 jetlejjetlej 5721 gold badge6 silver badges24 bronze badges 2
  • I would paste my code here but it's a very complex template for handling many post types. Can do a pastebin if required. – jetlej Commented Nov 24, 2014 at 2:28
  • Try wp_reset_postdata() or wp_reset_query(). If it doesn't work, please paste the full code of homepage template in pastebin. – Ashok Kumar Nath Commented Nov 24, 2014 at 7:33
Add a comment  | 

1 Answer 1

Reset to default 0

Switching out echo get_comments_number($post->ID) for echo $post->comment_count did the trick.

I have no idea why, that's exactly what the get_comments_number() function does...

本文标签: wp queryComment count same for every post in homepage WPQuery