返回介绍

comments_popup_link()

发布于 2017-09-10 21:46:30 字数 7376 浏览 1391 评论 0 收藏 0

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

Displays the link to the comments for the current post ID.


description


参数

$zero

(string) (Optional) String to display when no comments.

Default value: false

$one

(string) (Optional) String to display when only one comment is available.

Default value: false

$more

(string) (Optional) String to display when there are more than one comment.

Default value: false

$css_class

(string) (Optional) CSS class to use for comments.

Default value: ''

$none

(string) (Optional) String to display when comments have been turned off.

Default value: false


源代码

File: wp-includes/comment-template.php

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

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

	if ( false === $one ) {
		/* translators: %s: post title */
		$one = sprintf( __( '1 Comment<span class="screen-reader-text"> on %s</span>' ), $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 ), $title );
	}

	if ( false === $none ) {
		/* translators: %s: post title */
		$none = sprintf( __( 'Comments Off<span class="screen-reader-text"> on %s</span>' ), $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 integer $id The post ID.
		 */
		echo apply_filters( 'respond_link', $respond_link, $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>';
}

更新日志

Versiondescription
0.71Introduced.

相关函数

Uses

  • wp-includes/comment-template.php: respond_link
  • wp-includes/l10n.php: __()
  • wp-includes/l10n.php: _n()
  • wp-includes/l10n.php: _e()
  • wp-includes/formatting.php: esc_attr()
  • wp-includes/functions.php: number_format_i18n()
  • wp-includes/link-template.php: get_permalink()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/post-template.php: post_password_required()
  • wp-includes/post-template.php: get_the_ID()
  • wp-includes/post-template.php: get_the_title()
  • wp-includes/comment-template.php: comments_open()
  • wp-includes/comment-template.php: pings_open()
  • wp-includes/comment-template.php: comments_popup_link_attributes
  • wp-includes/comment-template.php: get_comments_number()
  • wp-includes/comment-template.php: comments_link()
  • wp-includes/comment-template.php: comments_number()
  • Show 12 more uses Hide more uses

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Text Response for Number of Comments
    Displays the comments popup link, using “No comments yet” for no comments, “1 comment” for one, “% comments” for more than one (% replaced bycomments-link is a custom CSS class for the link.

    
    <p>
    <?php
    comments_popup_link( 'No comments yet', '1 comment', '% comments', 'comments-link', 'Comments are off for this post');
    ?>
    </p>
    
    
  2. Hide Comment Link When Comments Are Deactivated
    Hides the paragraph element <p></p> that contains the comments_popup_link when comments are deactivated in the Write>Post screen. Good for those who want enable/disable comments post by post. Must be used in the loop.

    
    <?php
    if ( comments_open() ) :
    	echo '<p>';
    	comments_popup_link( 'No comments yet', '1 comment', '% comments', 'comments-link', 'Comments are off for this post');
    	echo '</p>';
    endif;
    ?>
    
    

    Load Different CSS classes according to Comment-condition
    If you want to load different classes into comments_popup_link(), use the following:

    
    $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' )
    );
    
    

    Text Response for Number of Comments with Localization

    Displays the comments popup link, using “No comments yet” for no comments, “1 comment” for one, “% comments” for more than one (% replaced by

    <?php comments_popup_link( __( 'Leave a comment', 'text-domain' ), __( '1 Comment', 'text-domain' ), __( '% Comments', 'text-domain' ) ); ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文