comments_link()
Outputs the link (URL) to the comment form for the current post.
This template tag should be used within the WordPress Loop.
Note: the result has already been sanitized for output using the function esc_url().
Uses: get_comments_link()
Used By: comments_popup_link()
1 time — 0.001856 sec (very slow) | 50000 times — 3.18 sec (fast) | PHP 7.1.5, WP 4.8.2
No Hooks.
Returns
null.
Usage
<?php comments_link(); ?>
Examples
#1 Demo
comments_link();
It displays on the screen:
- if the post has no comments:
http://example.com/postname#respond - if the post has comments:
http://example.com/postname#comments
#2 Display a link to the comment form of the current post in the WordPress Loop:
<a href="<?php comments_link(); ?>">Leave a comment</a>
If you apply this code to a category page, for example, you will get a "leave a comment" link for each post, which will take the user straight to the comment form on the post page.
Changelog
| Since 0.71 | Introduced. |
comments_link() comments link code WP 6.9.1
function comments_link( $deprecated = '', $deprecated_2 = '' ) {
if ( ! empty( $deprecated ) ) {
_deprecated_argument( __FUNCTION__, '0.72' );
}
if ( ! empty( $deprecated_2 ) ) {
_deprecated_argument( __FUNCTION__, '1.3.0' );
}
echo esc_url( get_comments_link() );
}