get_comment_link()WP 1.5.0

Gets a link to the specified comment. The link is generated considering the hierarchy. The link is returned with a hash at the end #comment-23.

Will return #comment-, not an empty string, if the specified comment does not exist.

echo get_comment_link( 6 ); // #comment-

Do not confuse with get_comments_link(), which returns a link (with anchor) to all comments for a specific post.

  • Uses: get_comment() to get the data of the passed comment $comment.

  • Uses global variables: $wp_rewrite and $in_comment_loop.
Used By: comment_link()
1 time — 0.002977 sec (very slow) | 50000 times — 5.21 sec (fast) | PHP 7.0.8, WP 4.6.1
Hooks from the function

Returns

String.

  • String — URL to the specified comment.
  • #comment- — If the comment does not exist.

The value is returned, not output to the screen.

Usage

$link = get_comment_link( $comment, $args );
$comment(int/object)
ID/object of the comment for which the link needs to be obtained.
Default: null (current comment in the loop)
$args(array)

Arguments. The following defaults are used:

  • cpage(int/string)
    The page number of pagination where the comment is located.

    If this value is specified, the current comment page number will not be calculated. Since version 4.4.

    If 0 is specified here, the URL will not have the part: /comment-page-1.
    Default: ''

  • page
    The page number of pagination where the comment is located. Used as a preliminary value for the cpage parameter before the comment page is calculated. Needed for backward compatibility, and ideally this parameter can be completely omitted.
    Default: 0

  • type
    Type of comment (not used directly).
    Default: 'all'

  • per_page
    Number of comments per pagination page.
    Default: 0

  • max_depth
    Max depth for hierarchical comments (not used directly).
    Default: ''

Default: array()

Examples

0

#1 An example of what the function gets

echo get_comment_link( 17 ); 
// http://example.com/postname/comment-page-1#comment-17

// If the comment does not exist
echo get_comment_link( 6 ); 
// #comment- 
0

#2 An example of the use of the HTML construct

<a href="<?php echo get_comments_link( $comment ); ?>">
	comments to the post
</a>
0

#3 Removing part of the comment pagination page from the URL

Suppose we know beforehand that the comment is on the first page and we set the parameter cpage=0 to avoid adding the extra /comment-page-1 in the URL (in this case the pagination page will not be calculated):

echo get_comment_link( $comm, [ 'cpage'=>0 ] ); 
// https://wp-kama.ru/question/vyvod-postov#comment-234
0

#4 Example of Comment Link on the Date in Comments

$posted_on = sprintf( __( 'Posted on %1$s at %2$s', 'textdomain' ), 
	get_comment_date( 'F j, Y' ), get_comment_time( 'g:ia' ) 
);

echo sprintf( '<a href="%1$s">%2$s</a>', esc_url( get_comment_link() ), $posted_on ); 

Notes

  • See: get_page_of_comment()
  • Global. WP_Rewrite. $wp_rewrite WordPress rewrite component.
  • Global. true|false. $in_comment_loop

Changelog

Since 1.5.0 Introduced.
Since 4.4.0 Added the ability for $comment to also accept a WP_Comment object. Added $cpage argument.

get_comment_link() code WP 6.8.3

function get_comment_link( $comment = null, $args = array() ) {
	global $wp_rewrite, $in_comment_loop;

	$comment = get_comment( $comment );

	// Back-compat.
	if ( ! is_array( $args ) ) {
		$args = array( 'page' => $args );
	}

	$defaults = array(
		'type'      => 'all',
		'page'      => '',
		'per_page'  => '',
		'max_depth' => '',
		'cpage'     => null,
	);

	$args = wp_parse_args( $args, $defaults );

	$comment_link = get_permalink( $comment->comment_post_ID );

	// The 'cpage' param takes precedence.
	if ( ! is_null( $args['cpage'] ) ) {
		$cpage = $args['cpage'];

		// No 'cpage' is provided, so we calculate one.
	} else {
		if ( '' === $args['per_page'] && get_option( 'page_comments' ) ) {
			$args['per_page'] = get_option( 'comments_per_page' );
		}

		if ( empty( $args['per_page'] ) ) {
			$args['per_page'] = 0;
			$args['page']     = 0;
		}

		$cpage = $args['page'];

		if ( '' === $cpage ) {
			if ( ! empty( $in_comment_loop ) ) {
				$cpage = (int) get_query_var( 'cpage' );
			} else {
				// Requires a database hit, so we only do it when we can't figure out from context.
				$cpage = get_page_of_comment( $comment->comment_ID, $args );
			}
		}

		/*
		 * If the default page displays the oldest comments, the permalinks for comments on the default page
		 * do not need a 'cpage' query var.
		 */
		if ( 'oldest' === get_option( 'default_comments_page' ) && 1 === $cpage ) {
			$cpage = '';
		}
	}

	if ( $cpage && get_option( 'page_comments' ) ) {
		if ( $wp_rewrite->using_permalinks() ) {
			if ( $cpage ) {
				$comment_link = trailingslashit( $comment_link ) . $wp_rewrite->comments_pagination_base . '-' . $cpage;
			}

			$comment_link = user_trailingslashit( $comment_link, 'comment' );
		} elseif ( $cpage ) {
			$comment_link = add_query_arg( 'cpage', $cpage, $comment_link );
		}
	}

	if ( $wp_rewrite->using_permalinks() ) {
		$comment_link = user_trailingslashit( $comment_link, 'comment' );
	}

	$comment_link = $comment_link . '#comment-' . $comment->comment_ID;

	/**
	 * Filters the returned single comment permalink.
	 *
	 * @since 2.8.0
	 * @since 4.4.0 Added the `$cpage` parameter.
	 *
	 * @see get_page_of_comment()
	 *
	 * @param string     $comment_link The comment permalink with '#comment-$id' appended.
	 * @param WP_Comment $comment      The current comment object.
	 * @param array      $args         An array of arguments to override the defaults.
	 * @param int        $cpage        The calculated 'cpage' value.
	 */
	return apply_filters( 'get_comment_link', $comment_link, $comment, $args, $cpage );
}