WordPress comments_number() Function

Beginner
4 min read
Updated: Nov 2026
3 examples
Template tag

Definition

In WordPress, comments_number() prints the number of comments of a post as text, such as 3 Comments.

You can set the text for no comments, one comment, and more than one comment. Use % in the last text where the number should go.

Syntax

Call it with no text for the defaults, or set your own texts.

WordPress
comments_number();
comments_number( 'No replies', 'One reply', '% replies' );
CallWhat it does
comments_number()Prints the count with default text, such as 3 Comments.
comments_number( 'No replies', 'One reply', '% replies' )Prints your own text for 0, 1, and more.
get_comments_number()Returns the count as a number.

Use get_comments_number() when you need the number for a condition.

Full declaration
WordPress
comments_number( string|false $zero = false, string|false $one = false, string|false $more = false, int|WP_Post $post = 0 ): void

Parameters

ParameterDescription
$zeroOptional. Text when there are no comments. Default No Comments.
$oneOptional. Text when there is one comment. Default 1 Comment.
$moreOptional. Text when there are more comments. Use % for the number. Default % Comments.
$postOptional. A post ID or WP_Post object. Default is the current post.

Return Value

Return ValueDescription
voidNothing is returned. The function prints the text.

WordPress Examples

Three short theme examples. Each one has a sample output.

Example 1 — Default text

Print the default text.

WordPress
<span class="count"><?php comments_number(); ?></span>

How It Works

  1. The post has three comments.
  2. WordPress prints the default text for more than one.
  3. The number replaces the percent sign.

Example 2 — Custom text

Use your own words for each case.

WordPress
<p><?php comments_number( 'No replies yet', 'One reply', '% replies' ); ?></p>

How It Works

  1. The first text is for zero comments.
  2. The second is for one comment.
  3. The third is for more, with % replaced by the number.

Example 3 — Use the number

Use get_comments_number() in a condition.

WordPress
<?php if ( get_comments_number() > 0 ) : ?>
  <h2><?php comments_number(); ?></h2>
<?php endif; ?>

How It Works

  1. get_comments_number() returns a number.
  2. The heading prints only when there are comments.
  3. comments_number() prints the text.

Frequently Asked Questions

It prints the number of comments of a post as text.
Pass your own text for zero, one, and more comments.
It is replaced by the number of comments.
Use get_comments_number().
Yes. Pass a post ID or a post object as the fourth argument.

Next: comments_popup_link()

Print a link to the comments of a post, with the count as text.

comments_popup_link() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful