^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 5年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云1核2g仅需102元/年,今日10点开抢

wordpress函数comments_popup_link()用法示例

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.


描述


参数

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

更新日志

Version 描述
0.71 Introduced.

相关函数

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

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈