get_comments_number_text( string $zero = false, string $one = false, string $more = false, int|WP_Post $post = 0 ): string
- Since
- 4.0.0, 5.4.0
- Source
wp-includes/comment-template.php:963
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
$zerostringoptional- Text for no comments. Default false.Default:
false $onestringoptional- Text for one comment. Default false.Default:
false $morestringoptional- 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
- Scaling
- Constant
- Instructions
- 22–64
- Plugin surface
- 1 hook
- Called by
- 2
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 99.
Third-party callbacks on 'comments_number' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
!$comments_number | 22–23 | get_comments_number(), apply_filters() |
!$comments_number | 26–27 | get_comments_number(), __(), apply_filters() |
$comments_number && $more !== false | 30 | get_comments_number(), _x(), number_format_i18n(), apply_filters() |
$comments_number && $more === false | 32 | get_comments_number(), _n(), number_format_i18n(), sprintf(), apply_filters() |
$comments_number && $more !== false | 41–45 | get_comments_number(), _x(), strip_tags(), number_format_i18n(), apply_filters() |
$comments_number && $more !== false && !$text && $more | 62–64 | get_comments_number(), _x(), strip_tags(), _n(), sprintf(), number_format_i18n(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 97 | 22–64 | 10 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 99 | 22–64 | 10 | |
| 8.4 | 99 | 22–64 | 10 | 28 fewer instructions than PHP 8.3 |
| 8.3 | 127 | 22–92 | 10 | |
| 8.2 | 127 | 22–92 | 10 | |
| 8.1 | 127 | 22–92 | 10 | |
| 7.4 | 127 | 22–92 | 10 |
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:
- apply_filters( comments_number )filterline 1016 (+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
- comments_number()Displays the language string for the number of comments the current post has.
- comments_popup_link()Displays the link to the comments for the current post ID.
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.
Signature, return type and hooks compared across 5 parsed releases.
$zero retyped from string to string|false.verified against source$one retyped from string to string|false.verified against source$more retyped from string to string|false.verified against source$post parameter to allow using the function outside of the loop.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.