small problem in the article ()

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

Topic template-tags templates Wordpress

Category Web


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.