WordPress函数:comments_popup_link 显示指向当前帖子ID的评论的链接

江河/ 2023年06月17日/ WordPress/ 浏览 1641

函数原型:


comments_popup_link( false|string $zero = false, false|string $one = false, false|string $more = false, string $css_class = '', false|string $none = false )


显示指向当前帖子ID的评论的链接。


参数说明:


$zero,没有评论时显示的字符串。默认值:false。


$one,只有一条评论可用时显示的字符串。默认值:false。


$more,当有多个评论时显示的字符串。默认值:false。


$css_class,用于评论的CSS类。默认值:“”。


$none,关闭评论时显示的字符串。默认值:false。


函数源码:


function comments_popup_link( $zero = false, $one = false, $more = false, $css_class = '', $none = false ) {
	$post_id    = get_the_ID();
	$post_title = get_the_title();
	$number     = get_comments_number( $post_id );

	if ( false === $zero ) {
		/* translators: %s: Post title. */
		$zero = sprintf( __( 'No Comments<span class="screen-reader-text"> on %s</span>' ), $post_title );
	}

	if ( false === $one ) {
		/* translators: %s: Post title. */
		$one = sprintf( __( '1 Comment<span class="screen-reader-text"> on %s</span>' ), $post_title );
	}

	if ( false === $more ) {
		/* translators: 1: Number of comments, 2: Post title. */
		$more = _n( '%1$s Comment<span class="screen-reader-text"> on %2$s</span>', '%1$s Comments<span class="screen-reader-text"> on %2$s</span>', $number );
		$more = sprintf( $more, number_format_i18n( $number ), $post_title );
	}

	if ( false === $none ) {
		/* translators: %s: Post title. */
		$none = sprintf( __( 'Comments Off<span class="screen-reader-text"> on %s</span>' ), $post_title );
	}

	if ( 0 == $number && ! comments_open() && ! pings_open() ) {
		echo '<span' . ( ( ! empty( $css_class ) ) ? ' class="' . esc_attr( $css_class ) . '"' : '' ) . '>' . $none . '</span>';
		return;
	}

	if ( post_password_required() ) {
		_e( 'Enter your password to view comments.' );
		return;
	}

	echo '<a href="';
	if ( 0 == $number ) {
		$respond_link = get_permalink() . '#respond';
		echo apply_filters( 'respond_link', $respond_link, $post_id );
	} else {
		comments_link();
	}
	echo '"';

	if ( ! empty( $css_class ) ) {
		echo ' class="' . $css_class . '" ';
	}

	$attributes = '';
	echo apply_filters( 'comments_popup_link_attributes', $attributes );

	echo '>';
	comments_number( $zero, $one, $more );
	echo '</a>';
}


包含钩子:


apply_filters( 'comments_popup_link_attributes', string $attributes )

apply_filters( 'respond_link', string $respond_link, int $post_id )


使用举例:


$css_class = 'zero-comments';
$number    = (int) get_comments_number( get_the_ID() );

if ( 1 === $number )
	$css_class = 'one-comment';
elseif ( 1 < $number )
	$css_class = 'multiple-comments';

comments_popup_link( 
	__( 'Post a Comment', 'wpdocs_textdomain' ), 
	__( '1 Comment', 'wpdocs_textdomain' ), 
	__( '% Comments', 'wpdocs_textdomain' ),
	$css_class,
	__( 'Comments are Closed', 'wpdocs_textdomain' )
);


发表评论

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

客服 工单