WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to display the number of comments a particular post has, so i thought i'd use comments_number, however when i use this the comment number gets printed outside of the span it's supposed to be contained within.

I tried using get_comments_number too, but that instead displays nothing at all.

The code below has been added to my functions.php amongst other code, but i just cannot get the comment number to display inside the span - it just prints at the top of the page.

$content .= '<p class="meta"><span class="date">'.$date.'</span><span class="comments">'. comments_number() .'</span></p>';
share|improve this question
up vote 2 down vote accepted

comments_number doesn't return the number of comments, it echos it out.

As the codex clearly states:

Use get_comments_number() to retrieve the value.

share|improve this answer
Seems there was an issue with how i was initially using get_comments_number - but yes, it works like a charm. – Nikki Mather Apr 10 '13 at 17:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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