comments_popup_link()
Displays the link to the comments for the current post ID.
1 time — 0.008033 sec (very slow) | 50000 times — 13.64 sec (slow) | PHP 7.1.5, WP 4.8.2
Hooks from the function
Return
null
. Nothing.
Usage
comments_popup_link( $zero, $one, $more, $css_class, $none );
- $zero(false|string)
- String to display when no comments.
Default: false - $one(false|string)
- String to display when only one comment is available.
Default: false - $more(false|string)
- String to display when there are more than one comment.
Default: false - $css_class(string)
- CSS class to use for comments.
Default: '' - $none(false|string)
- String to display when comments have been turned off.
Default: false
Examples
#1 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.
#2 How not to show the link to the comment popup when comments are closed: [auto-translate]
<?php if( comments_open() ){ comments_popup_link( 'No comments yet', '1 comment', '% comments', 'c_link' ); } ?>
#3 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' ) );
#4 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' ) ); ?>
Changelog
Since 0.71 | Introduced. |