comments_number()显示当前文章的评论数量

目录

描述

Displays the total number of comments, Trackbacks, and Pingbacks for the current post. This tag must be within The Loop.

Use get_comments_number() to retrieve the value.

comments_number() 描述

用法

 <?php comments_number$zero$one$more ); ?> 

comments_number() 用法

参数

$zero

(string) (可选) Text to display when there are no comments.

默认值: 'No Comments'

$one

(string) (可选) Text to display when there is one comment.

默认值: '1 Comment'

$more

(string) (可选) Text to display when there is more than one comment. % is replaced by the number of comments, so '% so far' is displayed as "5 so far" when there are five comments.

默认值: '% Comments'

comments_number() 参数

示例

Text Response to Number of Comments

Displays text based upon number of comments: Comment count zero - no responses; comment count one - one response; more than one comment (total 42) displays 42 responses.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<p>
  This post currently has
  <?php comments_number( 'no responses', 'one response', '% responses' ); ?>.
</p>

Title For Comments Section

You might want to have a title above your comments section that includes the number of comments. This example shows how to do that and have all the strings also be translatable.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<h3>
printf( _nx( 'One Comment', '%1$s Comments', get_comments_number(), 'comments title', 'textdomain' ), number_format_i18n( get_comments_number() ) );
</h3>

comments_number() 示例

历史

添加于 版本: 0.71

comments_number() 历史

源文件

comments_number() 函数的代码位于 wp-includes/comment-template.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Display the language string for the number of comments the current post has.
 *
 * @since 0.71
 *
 * @param string $zero       Optional. Text for no comments. Default false.
 * @param string $one        Optional. Text for one comment. Default false.
 * @param string $more       Optional. Text for more than one comment. Default false.
 * @param string $deprecated Not used.
 */
function comments_number( $zero = false, $one = false, $more = false, $deprecated = '' ) {
	if ( ! empty( $deprecated ) ) {
		_deprecated_argument( __FUNCTION__, '1.3' );
	}
	echo get_comments_number_text( $zero, $one, $more );
}

comments_number() 源文件

相关