comments_popup_link()WP 0.71

Outputs a ready <a> link to the comment popup, works when using the script comments_popup_script().

If the required script is not called, a simple link to the post comments will be output, see comments_link().

This template tag must be inside the WordPress Loop or the comments loop.

The script is deprecated since WP 4.5.0.

How the popup window will look is determined by the template file: comments-popup.php.

The function outputs nothing if individual post pages are displayed: is_single() or is_page().

1 time — 0.008033 sec (very slow) | 50000 times — 13.64 sec (slow) | PHP 7.1.5, WP 4.8.2

Returns

null.

Usage

comments_popup_link( $zero, $one, $more, $css_class, $none);
$zero(string)
Text to show when there are no comments.
Default: 'No comments'
$one(string)
Text to show if there is 1 comment.
Default: '1 comment'
$more(string)
Text to show if there is more than one comment.
Default: '%1$s comments'
$css_class(string)
Name of the css class for the link. By default, no class is set.
Default: ''
$none(string)
Text to show if commenting is disabled for the post.
Default: 'Commenting is disabled'

Examples

1

#1 How not to show the link to the comment popup when comments are closed:

<?php
if( comments_open() ){
	comments_popup_link( 'No comments yet', '1 comment', '% comments', 'c_link' );
}
?>
0

#2 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' )
);
0

#3 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 NUMBER 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' ) 
);
?>
-1

#4 Display a link to the post comments

<p><?php comments_popup_link(); ?></p>

We get:

<a href="https://example.com/post-name#respond">No Comments</a>

If post is password protected (see post_password_required(), then _e( 'Enter your password to view comments.' ) text will be shown.

Changelog

Since 0.71 Introduced.

comments_popup_link() code WP 6.9.1

function comments_popup_link( $zero = false, $one = false, $more = false, $css_class = '', $none = false ) {
	$post_id         = get_the_ID();
	$post_title      = get_the_title();
	$comments_number = (int) 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>',
			$comments_number
		);
		$more = sprintf( $more, number_format_i18n( $comments_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 === $comments_number && ! comments_open() && ! pings_open() ) {
		printf(
			'<span%1$s>%2$s</span>',
			! empty( $css_class ) ? ' class="' . esc_attr( $css_class ) . '"' : '',
			$none
		);
		return;
	}

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

	if ( 0 === $comments_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.
		 */
		$comments_link = apply_filters( 'respond_link', $respond_link, $post_id );
	} else {
		$comments_link = get_comments_link();
	}

	$link_attributes = '';

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

	printf(
		'<a href="%1$s"%2$s%3$s>%4$s</a>',
		esc_url( $comments_link ),
		! empty( $css_class ) ? ' class="' . $css_class . '" ' : '',
		$link_attributes,
		get_comments_number_text( $zero, $one, $more )
	);
}