get_comments_number_text()
云策文档标注
概述
get_comments_number_text() 函数用于生成并返回当前文章评论数量的本地化字符串,支持自定义无评论、单评论和多评论的文本。
关键要点
- 函数根据评论数量(0、1 或多于 1)返回相应的语言字符串,默认使用 WordPress 翻译系统。
- 参数 $zero、$one、$more 允许自定义文本,若为 false 则使用默认翻译。
- 参数 $post 可指定文章 ID 或 WP_Post 对象,默认使用全局 $post,支持在循环外使用。
- 返回值是经过 'comments_number' 过滤器处理的字符串,可用于显示评论数量。
- 在 $more 参数中,% 符号可用于插入评论数量,但 $one 参数不支持 %,需手动添加。
代码示例
$comments_text = get_comments_number_text( 'No comments', '1 comment found', '% comments found', $post_id );注意事项
- 注意 $one 参数不支持 % 符号,自定义时需手动包含评论数量。
- 函数内部使用 _n() 和 number_format_i18n() 处理复数形式和本地化数字格式。
原文内容
Displays the language string for the number of comments the current post has.
Parameters
Source
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 );
}
Hooks
- apply_filters( ‘comments_number’, string $comments_number_text, int $comments_number )
-
Filters the comments count for display.
Skip to note 2 content
Code Amp
A caveat to be aware of:
–
%symbol is supported in the$moreargument and can be used to position the comment count–
%symbol is not supported in the$onelabel – you have to add the comment count to the label yourselfEg:
$comments_text = get_comments_number_text( 'No comments', '1 comment found', '% comments found', $post_id );