WP 5.4 Theme masonic

I try to show the numer of comments in each article it'as ok (see the picture) but on the next line, itshow ()

I don't know why !?!?

the code in the file template-tags is:

echo '<div class="entry-author vcard author fa fa-user"><a class="url fn n" href="' . esc_url(get_author_posts_url(get_the_author_meta('ID'))) . '">' . esc_html(get_the_author()) . '</a></div>';

// Show number of comments
echo '<div>(' . comments_number( '(0) ', '(1)', '(%)' ) . ')</div>';

article 1

picture 2


There's two issues at play here.

First, you've got two sets of parentheses:

           ↓ Here                 ↓ And Here
echo '<div>(' . comments_number( '(0) ', '(1)', '(%)' ) . ')</div>';

You only want one.

Second, you're trying to concatenate a string (.) with a function, comments_number(), that echoes. This results in comments_number() being outut immediately, and then the rest of the string outputting after that. This is where the second () comes from.

To fix the issue, either remove echo and output like this:

comments_number( '<div>(0)</div>div>', '<div>(1)</div>div>', '<div>(%)</div>div>' ) . ');

Or use get_comments_number_text() which returns the same value:

echo '<div>(' . get_comments_number_text( '0', '1', '%' ) . ')</div>';

Note that I've also removed the redundant parentheses.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.