wppaste
WordPress

get_comments_number_text( string|false $zero = false, string|false $one = false, string|false $more = false, int|WP_Post $post = 0 ): string

Since
4.0.0, 5.4.0
Source
wp-includes/comment-template.php:959
Displays the language string for the number of comments the current post has.

Compatibility

WordPress
since 5.4.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$zerostring|falseoptional
Text for no comments. Default false.Default: false
$onestring|falseoptional
Text for one comment. Default false.Default: false
$morestring|falseoptional
Text for more than one comment. Default false.Default: false
$postint|WP_Postoptional
Post ID or WP_Post object. Default is the global $post.Default: 0

Return value

string
Language string for the number of comments a post has.

Performance profile

How much work a call to get_comments_number_text() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
22–64

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 99.

Plugin surface
1 hook

Third-party callbacks on 'comments_number' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_post()one call below get_comments_number_text()

Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 6 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_comments_number_text() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!$comments_number22–23get_comments_number(), apply_filters()
!$comments_number26–27get_comments_number(), __(), apply_filters()
$comments_number && $more !== false30get_comments_number(), _x(), number_format_i18n(), apply_filters()
$comments_number && $more === false32get_comments_number(), _n(), number_format_i18n(), sprintf(), apply_filters()
$comments_number && $more !== false41–45get_comments_number(), _x(), strip_tags(), number_format_i18n(), apply_filters()
$comments_number && $more !== false && !$text && $more62–64get_comments_number(), _x(), strip_tags(), _n(), sprintf(), number_format_i18n(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev9722–64102 fewer instructions than PHP 8.5
8.59922–6410
8.49922–641028 fewer instructions than PHP 8.3
8.312722–9210
8.212722–9210
8.112722–9210
7.412722–9210

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while get_comments_number_text() runs, in this order:

  1. apply_filters( comments_number )filterline 1012 (+53 into the body)

    Filters the comments count for display.

Uses · 7

  • get_comments_number()Retrieves the amount of comments a post has.
  • _n()Translates and retrieves the singular or plural form based on the supplied number.
  • number_format_i18n()Converts float number to format based on the locale.
  • _x()Retrieves translated string with gettext context.
  • str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
  • __()Retrieves the translation of $text.
  • apply_filters()Calls the callback functions that have been added to a filter hook.

Used by · 2

Source code

function get_comments_number_text( $zero = false, $one = false, $more = false, $post = 0 ) {	$comments_number = (int) get_comments_number( $post ); 	if ( $comments_number > 1 ) {		if ( false === $more ) {			$comments_number_text = sprintf(				/* translators: %s: Number of comments. */				_n( '%s Comment', '%s Comments', $comments_number ),				number_format_i18n( $comments_number )			);		} else {			// % Comments			/*			 * translators: If comment number in your language requires declension,			 * translate this to 'on'. Do not translate into your own language.			 */			if ( 'on' === _x( 'off', 'Comment number declension: on or off' ) ) {				$text = preg_replace( '#<span class="screen-reader-text">.+?</span>#', '', $more );				$text = preg_replace( '/&.+?;/', '', $text ); // Remove HTML entities.				$text = trim( strip_tags( $text ), '% ' ); 				// Replace '% Comments' with a proper plural form.				if ( $text && ! preg_match( '/[0-9]+/', $text ) && str_contains( $more, '%' ) ) {					/* translators: %s: Number of comments. */					$new_text = _n( '%s Comment', '%s Comments', $comments_number );					$new_text = trim( sprintf( $new_text, '' ) ); 					$more = str_replace( $text, $new_text, $more );					if ( ! str_contains( $more, '%' ) ) {						$more = '% ' . $more;					}				}			} 			$comments_number_text = str_replace( '%', number_format_i18n( $comments_number ), $more );		}	} elseif ( 0 === $comments_number ) {		$comments_number_text = ( false === $zero ) ? __( 'No Comments' ) : $zero;	} else { // Must be one.		$comments_number_text = ( false === $one ) ? __( '1 Comment' ) : $one;	} 	/**	 * Filters the comments count for display.	 *	 * @since 1.5.0	 *	 * @see _n()	 *	 * @param string $comments_number_text A translatable string formatted based on whether the count	 *                                     is equal to 0, 1, or 1+.	 * @param int    $comments_number      The number of post comments.	 */	return apply_filters( 'comments_number', $comments_number_text, $comments_number );}

Changelog

Introduced in 4.0.0. 3 changes between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.9.7
Parameter $zero retyped from string to string|false.verified against source
6.9.7
Parameter $one retyped from string to string|false.verified against source
6.9.7
Parameter $more retyped from string to string|false.verified against source
5.4.0
Added the $post parameter to allow using the function outside of the loop.from the docblock
4.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/comment-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.