get_comments_number_text( string $zero = false, string $one = false, string $more = false, int|WP_Post $post ): string
Displays the language string for the number of comments the current post has.
Parameters
Return
string Language string for the number of comments a post has.
Source
File: wp-includes/comment-template.php
.
View all references
function get_comments_number_text( $zero = false, $one = false, $more = false, $post = 0 ) {
$number = get_comments_number( $post );
if ( $number > 1 ) {
if ( false === $more ) {
/* translators: %s: Number of comments. */
$output = sprintf( _n( '%s Comment', '%s Comments', $number ), number_format_i18n( $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 ) && false !== strpos( $more, '%' ) ) {
/* translators: %s: Number of comments. */
$new_text = _n( '%s Comment', '%s Comments', $number );
$new_text = trim( sprintf( $new_text, '' ) );
$more = str_replace( $text, $new_text, $more );
if ( false === strpos( $more, '%' ) ) {
$more = '% ' . $more;
}
}
}
$output = str_replace( '%', number_format_i18n( $number ), $more );
}
} elseif ( 0 == $number ) {
$output = ( false === $zero ) ? __( 'No Comments' ) : $zero;
} else { // Must be one.
$output = ( false === $one ) ? __( '1 Comment' ) : $one;
}
/**
* Filters the comments count for display.
*
* @since 1.5.0
*
* @see _n()
*
* @param string $output A translatable string formatted based on whether the count
* is equal to 0, 1, or 1+.
* @param int $number The number of post comments.
*/
return apply_filters( 'comments_number', $output, $number );
}
Hooks
-
apply_filters( 'comments_number',
string $output ,int $number ) -
Filters the comments count for display.
Changelog
Version | Description |
---|---|
5.4.0 | Added the $post parameter to allow using the function outside of the loop. |
4.0.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
A caveat to be aware of:
–
%
symbol is supported in the$more
argument and can be used to position the comment count–
%
symbol is not supported in the$one
label – you have to add the comment count to the label yourselfEg: