How to show number of comments in WordPress

If you're looking to get more blog comments, you should display the number of comments in your blog to act as a social proof. You should also encourage and ask people to leave comments. Both of these can be done via simple hacks to your WordPress theme.

In this post, I'll show how to show number of comments in WordPress, and the next post shows specifically how to add call to action into comment links by modifying the "No comments" into something like "Start a discussion" instead.


I collected various ways to show the number of comments in WordPress to this post. If you're looking for ways to tweak your theme, create your own theme or otherwise need to use the number of comments, in a plugin for example, this post should have have what you need. If it doesn't - tell me, and I'll make sure it does.

If you edit the .php code of your theme, make sure to backup of all your theme files before doing any changes. If anything goes wrong, revert back to the original files.

When you edit the WordPress theme-files, or any other code, I recommend you edit them via FTP and use a good text editor with code-highlighting instead of "Appearance >> Editor" in the WordPress dashboard.

Number of Comments in WordPress Themes

Number of comments in WordPress can be displayed in many ways, including

  • template tag comments_popup_link, which also links to the comments.
  • template tag comments_number
  • function: get_comments_number
  • global variable: $post->comment_count
  • custom functions

The template tags are the simplest ways to show number of comments, so let's start with those...

Template Tag: comments_popup_link

This template tag must be within The Loop, or a comment loop, and it does nothing if  is_single() or  is_page() is true (even when within The Loop).

Or in plain English: the comments_popup_link does not work on individual posts or pages, only on homepage, or archives (e.g. author-, category- or tag-pages).

Template Tag: comments_number

WordPress also has comments_number template tag, which displays the number of comments, but doesn't link to the comments. In this case, comments_link template tag can used for the link.

By default, comments_number displays both the comments and trackbacks (also referred to as "pingbacks") with one number, unless the theme has functionality to separate trackbacks from comments.

Source: Stepping Into Template Tags at WordPress Codex.

Using the Template Tags to Show Number of Comments

There are two template tags in WordPress that can be used to display the number of comments:

  • comments_popup_link
  • comments_number

comments_popup_link displays a link to the comments popup window if comments_popup_script() is used (popup comments are relatively rare on WordPress blogs nowadays), otherwise it displays a normal link to comments. The default format for comments_popup_link is:

<?php comments_popup_link ('zero','one','more','CSSclass','none'); ?>

which inside WordPress theme is something like this:

<?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?>

  • zero: (string) Text to display when there are no comments.
    • Defaults to 'No Comments'.
  • one: (string) Text to display when there is one comment.
    • Defaults to '1 Comment'.
  • more: (string) Text to display when there are more than one comments. '%' is replaced by the number of comments, so '% so far' is displayed as "5 so far" when there are five comments.
    • Defaults to '% Comments'.
  • CSSclass: (string) CSS (stylesheet) class for the link.
    • This has no default value.
  • none: (string) Text to display when comments are disabled.
    • Defaults to 'Comments Off'.

The other template tag, comments_number, displays the number of comments without a link to the comments. It only uses the "zero", "one" and "more" (with the same defaults as above):

<?php comments_number('zero', 'one', 'more'); ?>

You can edit the "zero", "one" and "more" as you like, as you'll see just a bit later. When editing the theme-files, look for comments_popup_link or comments_number and you should find it.

You are likely to find it in theme files named (your theme does not necessary have all of these):

  • index.php
  • home.php
  • category.php
  • tag.php
  • archive.php
  • author.php

For example, you might see something this:

<?php comments_popup_link( __( 'Leave a comment', 'themename' ), __( '1 Comment', 'themename' ), __( '% Comments', 'themename' ) ) ?>

The "themename" is the name of the theme you are running, or the CSS class of the theme to be exact. If the "themename" is there in the comments link code, do not touch that part, leave it as is. It is related to styling and translation support of the theme.

For further information about these template tags and how to use them, go to the WordPress Codex:

Adding the Number of Comments to the WordPress Theme

It is possible that your template does not display the number of comments at all, or doesn't link to the comments, you should modify the theme and add theme, or change to a different theme altogether.

If you like your theme and don't want to change it and it doesn't have any links to comments by default, you need to add the comments_popup_link there yourself.

Comment Link on Home- or Archive-pages

Look into the files you want to add it into, e.g. index.php (or home.php) for the homepage. If the theme displays the_author, the_time or such, that's usually a good place to add the comments link and count as well.

For example, your theme might have something like this in there:

posted by <?php the_author(); ?> at <?php the_time(); ?>

And you could add the comments there by making it:

Posted by <?php the_author(); ?> at <?php the_time(); ?>. <?php comments_popup_link('Be the first to comment!', '1 comment.', '% comments already!'); ?>

Comment Link on an Individual Post

On individual posts, you can add the number of comments using the comment_number and comment_link instead, like this:

<a href="<?php comments_link(); ?>"><?php comments_number('Be the first to comment!', '1 comment.', '% comments already!'); ?></a>

Showing Number of Comments without the Link

If you just want to display the number of comments, without the link:

<?php comments_number('0', '1', '%'); ?>

For CSS-styling, you want to place that inside some tags, like

<span class="comment-number"><?php comments_number('0', '1', '%'); ?></span>


Using this function is relatively simple, for example:

<?php $commentscount = get_comments_number(); echo $commentscount; ?>

Global Variable: $post->comment_count

$post is a global variable, which theme authors do not need to use, but it's there if needed, set in the method the_post.

For example:
echo 'comment count is: ' . $post->comment_count;

Sources and further reading:

Custom Functions

Number of comments can be counted in many ways using custom functions, but for optimization purposes you should only use these when the default methods are not enough.

Common usage for this is separating trackbacks (pingbacks) from other comments:
<?php /* Count the number of comments and trackbacks (or pings) */
$ping_count = $comment_count = 0;
foreach ( $comments as $comment )
get_comment_type() == "comment" ? ++$comment_count : ++$ping_count;

Which you can then use, like this (not displaying anything if the count is o):
<?php if ( ! empty($comments_by_type['pings']) ) : ?>
<?php printf($ping_count > 1 ? __('%d trackbacks') : __('One trackback'), $ping_count) ?>
<?php endif; ?>

and similarly for the comments:
<?php if ( ! empty($comments_by_type['comment']) ) : ?>
<?php printf($comment_count > 1 ? __('%d comments') : __('one comment'), $comment_count) ?>
<?php endif; ?>

Source: The WordPress Theme Comments Template at The Ultimate WordPress Theme Tutorial (an awesome tutorial I might add) by Ian Stewart (the man behind the Thematic Theme Framework for example).

Number of Comments in the Comments Section

The comments are displayed via the code in the comments.php -file in the WordPress theme and number of comments can be displayed there by many different means covered above (check the theme tutorial for inspiration).

Some themes use the comments_number, but since that includes both comments and trackbacks, some themes separate the trackbacks from normal comments, and have custom functions and hooks to calculate the counts and display the numbers.


There are many ways to show number of comments in WordPress, like

  • template tags
    • comments_popup_link
    • comments_number
  • via get_comments_number function
  • via global variable: $post
    • post->comment_count
  • custom functions

Choose the way depending on what you are trying to achieve with the code. For most situations, using the template tags is enough.

Tell me how can I make this post and blog better to help you even more! If you have any comments or questions about this post or other WordPress hacks, theme related or not, feel free to ask your questions in the comments or send me an email using the contact form.

Posted by

Topic: WordPress
Tags: , , , ,

If you enjoyed this post, sign up for updates (it's free)

Feedback, questions and comments are also welcome on my Facebook page