WordPress函数:comments_number 显示当前文章的评论个数

江河/ 2023年06月16日/ WordPress/ 浏览 1436

函数原型:


comments_number( string|false $zero = false, string|false $one = false, string|false $more = false, int|WP_Post $post )


显示当前文章的评论个数。


参数说明:


$zero,无评论的文本。默认值:false。


$one,一条评论的文本。默认值:false。


$more,多条评论的文本。默认值:false。


$post,文章ID 或 文章对象,默认当前文章。


函数源码:


function comments_number( $zero = false, $one = false, $more = false, $post = 0 ) {
	echo get_comments_number_text( $zero, $one, $more, $post );
}


使用举例:


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


同名钩子 comments_number 的使用。


add_filter( 'comments_number', 'wporg_com_num', 10, 2 );
function wporg_com_num ( $out, $num ) { // Two parameter as in filter described
    if ( 0 === $num ) { 
        $out = '0 Comments'; // If No comments
    } elseif ( 1 === $num ) {
        $out = '1 Comment'; // If 1 comment
    } else {
        $out = $num . ' Comments'; // More than 1 comment
    }

    return $out;
}


发表评论

暂无评论,抢个沙发...

客服 工单