get_comments_link()
Gets the link (URL) to all comments for the current post.
Uses: get_permalink()
Used By: comments_link()
Hooks from the function
Returns
String. URL for comments.
Usage
get_comments_link( $post_id );
- $post_id(int/WP_Post)
- ID or post object. By default, the global variable $post.
Default: 0 (current post)
Examples
#1 Get link to the comment
Suppose the function is called on a page with a URL: http://example.com/page, then:
// if the post has no comments: echo get_comments_link(); //> http://example.com/page#respond // if the post has comments: echo get_comments_link(); //> http://example.com/page#comments
Changelog
| Since 1.5.0 | Introduced. |
get_comments_link() get comments link code WP 6.8.3
function get_comments_link( $post = 0 ) {
$hash = get_comments_number( $post ) ? '#comments' : '#respond';
$comments_link = get_permalink( $post ) . $hash;
/**
* Filters the returned post comments permalink.
*
* @since 3.6.0
*
* @param string $comments_link Post comments permalink with '#comments' appended.
* @param int|WP_Post $post Post ID or WP_Post object.
*/
return apply_filters( 'get_comments_link', $comments_link, $post );
}