I am customizing a theme that has the following code to display comments:
if ( have_comments() ) : ?>
<h2 class="comments-title">
<?php
printf( // WPCS: XSS OK.
esc_html( _nx( 'One thought on “%2$s”', '%1$s thoughts on “%2$s”', get_comments_number(), 'comments title', 'kadabra' ) ),
number_format_i18n( get_comments_number() ),
'<span>' . get_the_title() . '</span>'
);
?>
</h2>
however it always shows the following:
0 thoughts on “Post title”
even though I have several comments and the if ( have_comments() ) :
part is passed. Any ideas?
PS: wp_debug is enabled and show no errors as well.
I am customizing a theme that has the following code to display comments:
if ( have_comments() ) : ?>
<h2 class="comments-title">
<?php
printf( // WPCS: XSS OK.
esc_html( _nx( 'One thought on “%2$s”', '%1$s thoughts on “%2$s”', get_comments_number(), 'comments title', 'kadabra' ) ),
number_format_i18n( get_comments_number() ),
'<span>' . get_the_title() . '</span>'
);
?>
</h2>
however it always shows the following:
0 thoughts on “Post title”
even though I have several comments and the if ( have_comments() ) :
part is passed. Any ideas?
PS: wp_debug is enabled and show no errors as well.
Share Improve this question asked Jul 3, 2017 at 16:27 Deka87Deka87 1319 bronze badges 2 |2 Answers
Reset to default 1From quick look at the source there seems to be three possibilities:
get_post()
returned falsy value, so current post context is invalid in some way.$post->comment_count
is0
.get_comments_number
filter is being used to adjust the output.
Most commonly it would be case 1/2 with something interfering with global post context, dump get_post()
at the point and see if it contains expected instance.
What probably happened is that you did not approve comments in admin panel.
When you only have comments that are "pending" you still pass have_comments()
, but those comments are not counted.
get_comments_number($post)
maybe? although ifhave_comments
andget_the_title
are both working without a post object/id in them then that should as well. do you still get 0 if youvar_dump
comments_number? – mrben522 Commented Jul 3, 2017 at 17:22