Technology WordPress Tips & Tricks

Fetching the Comments Count on Single Post

Sometimes you need the comment count on a single post for a custom layout or feature. Here is how to fetch it in WordPress, a small snippet that comes in handy more often than you would expect.

Omkar Bhagat Omkar Bhagat 2 min read
Fetching the Comments Count on Single Post

Many a times we need to fetch the total number of comments on a single post or display that number on the excerpt. It seems pretty easy but actually there are many ways to fetch the number of comments in WordPress. For instance –

  • Template tags

    • comments_popup_link

    • comments_number

  • via get_comments_number function

  • via global variable: $post

    • post->comment_count

  • other custom ways

Now the comments_popup_link works only inside the loop and doesn’t really work on is_single() or is_page(). In simple terms, it works only on homepage or archive pages. So let’s not talk about this function.

[space]

Methods which can be used

The comments_number function will return the number of comments and can be used as follows:

[pre]<?php comments_number(‘zero’, ‘one’, ‘more’); ?>[/pre]

Another way is by using post->comment_count as shown below

[pre]<?php echo ‘comment count is: ‘ . $post->comment_count; ?>[/pre]

[space]

Preferred Method To Add Comments Number

But these are all somewhat complex and relatively difficult to customize or implement. The most easiest way is to use get_comments_number function.

[pre]<?php $commentscount = get_comments_number(); echo $commentscount; ?>[/pre]

You can customize this comment count. For example –

[pre]<?php
$commentscount = get_comments_number();
echo ‘(‘ . $commentscount . ‘)’ . ‘ Comments’;
?>[/pre]

 

This will display the comments as follows –

[pre](0) Comments
(1) Comments
(2) Comments[/pre]

 

You can see, how easy it is to implement get_comments_number() function. Also it’s very simple to customize it with a bit of PHP code.

And hey, I’m sure some of you are wondering about the 1 Comment’S’ part. Well we can of course change that 1 ‘commentS’ to 1 comment using an If Else code.

Get a FREE Consultation

Let's build something that lasts.

Share what's on your mind — a clear brief, a half-formed idea, or just a sense that something needs to change. We'll listen first, ask the right questions, and point you toward what's actually worth building.

We take on a handful of projects each quarter,ones where we can truly make a difference.

  • Receive a human response within 24 hours
  • Get a detailed scope and quote upfront
  • We're happy to sign an NDA upon request

    Free 30-Min Strategy Call

    Your Name *

    Your Phone No *

    Work Email *

    Your Budget*

    Project Details *