get_post_reply_link()
云策文档标注
概述
get_post_reply_link() 函数用于生成回复文章链接的 HTML 内容,支持自定义参数以控制链接文本、样式和行为。该函数检查评论是否开启和用户登录状态,返回链接或 false。
关键要点
- 函数返回回复文章链接的 HTML 字符串,若评论关闭则返回 false。
- 参数 $args 为可选数组,可覆盖默认设置,包括 add_below、respond_id、reply_text、login_text、before 和 after。
- 参数 $post 为可选,指定文章 ID 或 WP_Post 对象,默认为当前文章。
- 内部逻辑基于 comments_open() 和 get_option('comment_registration') 判断是否显示链接或登录提示。
- 使用 apply_filters('post_comments_link', $post_reply_link, $post) 钩子允许过滤输出。
代码示例
function get_post_reply_link( $args = array(), $post = null ) {
$defaults = array(
'add_below' => 'post',
'respond_id' => 'respond',
'reply_text' => __( 'Leave a Comment' ),
'login_text' => __( 'Log in to leave a Comment' ),
'before' => '',
'after' => '',
);
$args = wp_parse_args( $args, $defaults );
$post = get_post( $post );
if ( ! comments_open( $post->ID ) ) {
return false;
}
if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) {
$link = sprintf(
'%s',
wp_login_url( get_permalink() ),
$args['login_text']
);
} else {
$onclick = sprintf(
'return addComment.moveForm( "%1$s-%2$s", "0", "%3$s", "%2$s" )',
$args['add_below'],
$post->ID,
$args['respond_id']
);
$link = sprintf(
"%s",
get_permalink( $post->ID ) . '#' . $args['respond_id'],
$onclick,
$args['reply_text']
);
}
$post_reply_link = $args['before'] . $link . $args['after'];
return apply_filters( 'post_comments_link', $post_reply_link, $post );
}注意事项
- 确保在调用前文章评论功能已开启,否则函数返回 false。
- 参数 $args 中的字符串值需根据主题需求自定义,以适配多语言或特定样式。
- 钩子 post_comments_link 可用于修改最终输出的 HTML,便于扩展功能。
原文内容
Retrieves HTML content for reply to post link.
Parameters
$argsarrayoptional-
Override default arguments.
add_belowstringThe first part of the selector used to identify the comment to respond below.
The resulting value is passed as the first parameter to addComment.moveForm(), concatenated as $add_below-$comment->comment_ID. Default is'post'.respond_idstringThe selector identifying the responding comment. Passed as the third parameter to addComment.moveForm(), and appended to the link URL as a hash value.
Default'respond'.reply_textstringText of the Reply link. Default is ‘Leave a Comment’.login_textstringText of the link to reply if logged out. Default is ‘Log in to leave a Comment’.beforestringText or HTML to add before the reply link.afterstringText or HTML to add after the reply link.
Default:
array() $postint|WP_Postoptional-
Post ID or WP_Post object the comment is going to be displayed on.
Default current post.Default:
null
Source
function get_post_reply_link( $args = array(), $post = null ) {
$defaults = array(
'add_below' => 'post',
'respond_id' => 'respond',
'reply_text' => __( 'Leave a Comment' ),
'login_text' => __( 'Log in to leave a Comment' ),
'before' => '',
'after' => '',
);
$args = wp_parse_args( $args, $defaults );
$post = get_post( $post );
if ( ! comments_open( $post->ID ) ) {
return false;
}
if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) {
$link = sprintf(
'<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',
wp_login_url( get_permalink() ),
$args['login_text']
);
} else {
$onclick = sprintf(
'return addComment.moveForm( "%1$s-%2$s", "0", "%3$s", "%2$s" )',
$args['add_below'],
$post->ID,
$args['respond_id']
);
$link = sprintf(
"<a rel='nofollow' class='comment-reply-link' href='%s' onclick='%s'>%s</a>",
get_permalink( $post->ID ) . '#' . $args['respond_id'],
$onclick,
$args['reply_text']
);
}
$post_reply_link = $args['before'] . $link . $args['after'];
/**
* Filters the formatted post comments link HTML.
*
* @since 2.7.0
*
* @param string $post_reply_link The HTML-formatted post comments link.
* @param int|WP_Post $post The post ID or WP_Post object.
*/
return apply_filters( 'post_comments_link', $post_reply_link, $post );
}
Hooks
- apply_filters( ‘post_comments_link’, string $post_reply_link, int|WP_Post $post )
-
Filters the formatted post comments link HTML.
Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |