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

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


Description Description


Parameters Parameters

$zero

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

Default value: false

$one

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

Default value: false

$more

(false|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

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

Default value: false


Top ↑

Source Source

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

Top ↑

Changelog Changelog

Changelog
Version Description
0.71 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by DannyCooper

    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 # of comments), and “Comments are off for this post” if commenting is disabled. Additionally, comments-link is a custom CSS class for the link.

    <?php comments_popup_link( __( 'Leave a comment', 'text-domain' ), __( '1 Comment', 'text-domain' ), __( '% Comments', 'text-domain' ) ); ?>
  2. Skip to note 2 content
    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 by # of comments), and “Comments are off for this post” if commenting is disabled. Additionally, comments-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>
    
    
  3. Skip to note 3 content
    Contributed by Codex

    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;
    ?>
    
    
  4. Skip to note 4 content
    Contributed by Codex

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

You must log in before being able to contribute a note or feedback.