get_comments_number_text() WordPress Function

The get_comments_number_text() function is used to display the number of comments for a post. This function takes two arguments: the first is the number of comments, and the second is the text to display when there are no comments. The default value for the second argument is "No Comments", but this can be changed to any string you like.

get_comments_number_text( string $zero = false, string $one = false, string $more = false, int|WP_Post $post_id ) #

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


Parameters

$zero

(string)(Optional) Text for no comments.

Default value: false

$one

(string)(Optional) Text for one comment.

Default value: false

$more

(string)(Optional) Text for more than one comment.

Default value: false

$post_id

(int|WP_Post)(Optional) Post ID or WP_Post object. Default is the global $post.


Top ↑

Return

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


Top ↑

Source

File: wp-includes/comment-template.php

function get_comments_number_text( $zero = false, $one = false, $more = false, $post_id = 0 ) {
	$number = get_comments_number( $post_id );

	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 ); // Kill 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 );
}


Top ↑

Changelog

Changelog
VersionDescription
5.4.0Added the $post_id parameter to allow using the function outside of the loop.
4.0.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More