The get_comment_link() WordPress PHP function retrieves the link to a given comment.
Usage
echo get_comment_link( $comment, $args );
Parameters
$comment
(WP_Comment|int|null) – Optional. Comment to retrieve. Default current comment. Default: null.$args
(array) – Optional. An array of optional arguments to override the defaults:type
(string) – Passed to get_page_of_comment(). Limit paginated comments to those matching a given type. Accepts ‘comment’, ‘trackback’, ‘pingback’, ‘pings’ (trackbacks and pingbacks), or ‘all’. Default ‘all’.page
(int) – Current page of comments, for calculating comment pagination.per_page
(int) – Per-page value for comment pagination.max_depth
(int) – Passed to get_page_of_comment(). If greater than 1, comment page will be determined for the top-level parent$comment_id
. Defaults to the value of the ‘thread_comments_depth’ option.cpage
(int|string) – Value to use for the comment’s “comment-page” or “cpage” value. If provided, this value overrides any value calculated from$page
and$per_page
.
More information
See WordPress Developer Resources: get_comment_link
Examples
Display a comment link
This example displays the comment link for the current comment in a loop.
echo '<a href="' . esc_url( get_comment_link() ) . '">View Comment</a>';
Get comment link for a specific comment
This example retrieves the comment link for a specific comment by its ID.
$comment_id = 123; $comment = get_comment( $comment_id ); echo '<a href="' . esc_url( get_comment_link( $comment ) ) . '">View Comment</a>';
Display comment link with custom arguments
This example displays the comment link for the current comment with custom arguments.
$args = array( 'type' => 'comment', 'page' => 2, 'per_page' => 10 ); echo '<a href="' . esc_url( get_comment_link( null, $args ) ) . '">View Comment</a>';
Display comment link with a specific depth
This example displays the comment link for the current comment with a specific depth.
$args = array( 'max_depth' => 3 ); echo '<a href="' . esc_url( get_comment_link( null, $args ) ) . '">View Comment</a>';
Display comment link on the date in comments
This example shows how to display the 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' ) ); printf( '<a href="%1$s">%2$s</a>', esc_url( get_comment_link() ), $posted_on );