comments_popup_link

函数
comments_popup_link ( $zero = false, $one = false, $more = false, $css_class = '', $none = false )
参数
  • (false|string) $zero Optional. String to display when no comments. Default false.
    Required:
    Default: false
  • (false|string) $one Optional. String to display when only one comment is available. Default false.
    Required:
    Default: false
  • (false|string) $more Optional. String to display when there are more than one comment. Default false.
    Required:
    Default: false
  • (string) $css_class Optional. CSS class to use for comments. Default empty.
    Required:
    Default: (empty)
  • (false|string) $none Optional. String to display when comments have been turned off. Default false.
    Required:
    Default: false
定义位置
相关方法
comments_linkcomments_popup_scriptis_comments_popupcomments_rss_linkcomment_link
引入
0.71
弃用
-

comments_popup_link: 这个函数生成一个指向文章的评论区的链接。链接文本由评论的数量决定,点击该链接会使用户进入评论区。

显示当前文章ID的评论链接。

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';
		/**
		 * Filters the respond link when a post has no comments.
		 *
		 * @since 4.4.0
		 *
		 * @param string $respond_link The default response link.
		 * @param int    $post_id      The post ID.
		 */
		echo apply_filters( 'respond_link', $respond_link, $post_id );
	} else {
		comments_link();
	}
	echo '"';

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

	$attributes = '';
	/**
	 * Filters the comments link attributes for display.
	 *
	 * @since 2.5.0
	 *
	 * @param string $attributes The comments link attributes. Default empty.
	 */
	echo apply_filters( 'comments_popup_link_attributes', $attributes );

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

常见问题

FAQs
查看更多 >