get_comment_link()
云策文档标注
概述
get_comment_link() 函数用于获取指定评论的永久链接,支持通过参数控制评论分页和类型过滤。该函数内部处理评论分页逻辑,并返回包含锚点的完整 URL。
关键要点
- 函数返回字符串类型的评论永久链接,格式通常为文章链接加上评论分页和评论 ID 锚点(如 #comment-123)。
- 参数 $comment 可接受 WP_Comment 对象、评论 ID 或 null(默认当前评论)。
- 参数 $args 为数组,可选覆盖默认设置,包括 type、page、per_page、max_depth 和 cpage 等,用于控制分页计算和评论类型过滤。
- 函数内部依赖 get_page_of_comment() 计算分页,并考虑 WordPress 设置如 page_comments、comments_per_page 和 default_comments_page。
- 提供 apply_filters('get_comment_link', ...) Hook,允许开发者过滤返回的链接。
代码示例
$posted_on = sprintf( __( 'Posted on %1$s at %2$s', 'textdomain' ), get_comment_date( 'F j, Y' ), get_comment_time( 'g:ia' ) );
printf( '%2$s', esc_url( get_comment_link() ), $posted_on );
原文内容
Retrieves the link to a given comment.
Description
See also
Parameters
$commentWP_Comment|int|nulloptional-
Comment to retrieve. Default current comment.
Default:
null $argsarrayoptional-
An array of optional arguments to override the defaults.
typestringPassed to get_page_of_comment() .pageintCurrent page of comments, for calculating comment pagination.per_pageintPer-page value for comment pagination.max_depthintPassed to get_page_of_comment() .cpageint|stringValue to use for the comment’s “comment-page” or “cpage” value.
If provided, this value overrides any value calculated from$pageand$per_page.
More Arguments from get_page_of_comment( … $args )
Array of optional arguments.
typestringLimit paginated comments to those matching a given type.
Accepts'comment','trackback','pingback','pings'(trackbacks and pingbacks), or'all'. Default'all'.per_pageintPer-page count to use when calculating pagination.
Defaults to the value of the'comments_per_page'option.max_depthint|stringIf 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.
Default:
array()
Source
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() ) {
$comment_link = trailingslashit( $comment_link ) . $wp_rewrite->comments_pagination_base . '-' . $cpage;
} else {
$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 );
}
Hooks
- apply_filters( ‘get_comment_link’, string $comment_link, WP_Comment $comment, array $args, int $cpage )
-
Filters the returned single comment permalink.
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Added the ability for $comment to also accept a WP_Comment object. Added $cpage argument. |
| 1.5.0 | Introduced. |
Skip to note 2 content
Rose
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' ) ); printf( '<a href="%1$s">%2$s</a>', esc_url( get_comment_link() ), $posted_on );