get_comments_link()WP 1.5.0

Retrieves the link to the current post comments.

Used By: comments_link()
Hooks from the function

Return

String. The link to the comments.

Usage

get_comments_link( $post );
$post(int|WP_Post)
Post ID or WP_Post object.
Default: global $post

Examples

0

#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() code WP 6.5.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 );
}