comments_number( string|false $zero = false, string|false $one = false, string|false $more = false, int|WP_Post $post )

Displays the language string for the number of comments the current post has.

Parameters

$zerostring|falseoptional
Text for no comments.

Default:false

$onestring|falseoptional
Text for one comment.

Default:false

$morestring|falseoptional
Text for more than one comment.

Default:false

$postint|WP_Postoptional
Post ID or WP_Post object. Default is the global $post.

Source

function comments_number( $zero = false, $one = false, $more = false, $post = 0 ) {
	echo get_comments_number_text( $zero, $one, $more, $post );
}

Changelog

VersionDescription
5.4.0The $deprecated parameter was changed to $post.
0.71Introduced.

User Contributed Notes

  1. Skip to note 4 content

    Text Response to Number of Comments

    Displays text based upon number of comments: Comment count zero – no responses; comment count one – one response; more than one comment (total 42) displays 42 responses.

    <p>
      This post currently has
      <?php comments_number( 'no responses', 'one response', '% responses' ); ?>.
    </p>
  2. Skip to note 5 content

    Title For Comments Section

    You might want to have a title above your comments section that includes the number of comments. This example shows how to do that and have all the strings also be translatable.

    <h3>
    printf( _nx( 'One Comment', '%1$s Comments', get_comments_number(), 'comments title', 'textdomain' ), number_format_i18n( get_comments_number() ) );
    </h3>
  3. Skip to note 6 content

    Usage of ‘comments_number’ filter

    add_filter( 'comments_number', 'wporg_com_num', 10, 2 );
    function wporg_com_num ( $out, $num ) { // Two parameter as in filter described
        if ( 0 === $num ) { 
            $out = '0 Comments'; // If No comments
        } elseif ( 1 === $num ) {
            $out = '1 Comment'; // If 1 comment
        } else {
            $out = $num . ' Comments'; // More than 1 comment
        }
    
        return $out;
    }

You must log in before being able to contribute a note or feedback.