get_comments_number_text

函式
get_comments_number_text ( $zero = false, $one = false, $more = false, $post = 0 )
引數
  • (string) $zero Optional. Text for no comments. Default false.
    Required:
    Default: false
  • (string) $one Optional. Text for one comment. Default false.
    Required:
    Default: false
  • (string) $more Optional. Text for more than one comment. Default false.
    Required:
    Default: false
  • (int|WP_Post) $post Optional. Post ID or WP_Post object. Default is the global `$post`.
    Required:
返回值
  • (string) Language string for the number of comments a post has.
定義位置
相關方法
get_comments_numberget_comment_textcomments_numberget_comment_metaget_comments_popup_template
引入
4.0.0
棄用
-

get_comments_number_text: 這個函式用來檢索一個文章或頁面的評論總數所要顯示的文字。它返回一個字串值,如’No Comments’, ‘1 Comment’, ‘2 Comments’, 等等。

顯示當前文章的評論數量的語言字串。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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 ); // 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 );
}
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 ); // 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 ); }
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 ); // 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 );
}

常見問題

FAQs
檢視更多 >