comments_popup_link()
云策文档标注
概述
comments_popup_link() 函数用于显示当前文章的评论链接,支持根据评论数量自定义显示文本,并可应用 CSS 类。它处理评论关闭、密码保护等场景,并包含多个可过滤的 Hook。
关键要点
- 函数显示当前文章的评论链接,基于评论数量(零、一或多)和评论状态(如关闭)输出不同文本。
- 接受五个可选参数:$zero(无评论时文本)、$one(一条评论时文本)、$more(多条评论时文本)、$css_class(CSS 类名)、$none(评论关闭时文本),默认值均为 false 以使用本地化默认文本。
- 内部逻辑包括检查评论是否开放、文章是否需要密码,并调用 get_comments_number_text() 生成文本。
- 提供两个 Hook:comments_popup_link_attributes 用于过滤链接属性,respond_link 用于过滤无评论时的响应链接。
- 函数依赖于多个 WordPress 核心函数,如 get_the_ID()、get_comments_number() 和 get_comments_link()。
代码示例
// 基本用法,使用默认文本
comments_popup_link();
// 自定义文本和 CSS 类
comments_popup_link('暂无评论', '1 条评论', '% 条评论', 'my-comments-class', '评论已关闭');
// 根据评论数量动态设置 CSS 类(来自用户贡献笔记)
$css_class = 'zero-comments';
$number = (int) get_comments_number(get_the_ID());
if (1 === $number) {
$css_class = 'one-comment';
} elseif (1 < $number) {
$css_class = 'multiple-comments';
}
comments_popup_link(false, false, false, $css_class, false);注意事项
- 函数应在 WordPress 循环内使用,以确保正确获取当前文章 ID。
- 参数 $zero、$one、$more 和 $none 若为 false,则使用本地化默认文本,支持翻译。
- 当评论关闭且 ping 也关闭时,会显示 $none 文本并直接返回,不输出链接。
- 文章受密码保护时,显示密码提示并返回,不输出评论链接。
- 可通过 Hook 自定义链接属性或响应链接,增强灵活性。
原文内容
Displays the link to the comments for the current post ID.
Parameters
$zerofalse|stringoptional-
String to display when no comments.
Default:
false $onefalse|stringoptional-
String to display when only one comment is available.
Default:
false $morefalse|stringoptional-
String to display when there are more than one comment.
Default:
false $css_classstringoptional-
CSS class to use for comments. Default empty.
$nonefalse|stringoptional-
String to display when comments have been turned off.
Default:
false
Source
function comments_popup_link( $zero = false, $one = false, $more = false, $css_class = '', $none = false ) {
$post_id = get_the_ID();
$post_title = get_the_title();
$comments_number = (int) get_comments_number( $post_id );
if ( false === $zero ) {
/* translators: %s: Post title. */
$zero = sprintf( __( 'No Comments<span class="screen-reader-text"> on %s</span>' ), $post_title );
}
if ( false === $one ) {
/* translators: %s: Post title. */
$one = sprintf( __( '1 Comment<span class="screen-reader-text"> on %s</span>' ), $post_title );
}
if ( false === $more ) {
/* translators: 1: Number of comments, 2: Post title. */
$more = _n(
'%1$s Comment<span class="screen-reader-text"> on %2$s</span>',
'%1$s Comments<span class="screen-reader-text"> on %2$s</span>',
$comments_number
);
$more = sprintf( $more, number_format_i18n( $comments_number ), $post_title );
}
if ( false === $none ) {
/* translators: %s: Post title. */
$none = sprintf( __( 'Comments Off<span class="screen-reader-text"> on %s</span>' ), $post_title );
}
if ( 0 === $comments_number && ! comments_open() && ! pings_open() ) {
printf(
'<span%1$s>%2$s</span>',
! empty( $css_class ) ? ' class="' . esc_attr( $css_class ) . '"' : '',
$none
);
return;
}
if ( post_password_required() ) {
_e( 'Enter your password to view comments.' );
return;
}
if ( 0 === $comments_number ) {
$respond_link = get_permalink() . '#respond';
/**
* Filters the respond link when a post has no comments.
*
* @since 4.4.0
*
* @param string $respond_link The default response link.
* @param int $post_id The post ID.
*/
$comments_link = apply_filters( 'respond_link', $respond_link, $post_id );
} else {
$comments_link = get_comments_link();
}
$link_attributes = '';
/**
* Filters the comments link attributes for display.
*
* @since 2.5.0
*
* @param string $link_attributes The comments link attributes. Default empty.
*/
$link_attributes = apply_filters( 'comments_popup_link_attributes', $link_attributes );
printf(
'<a href="%1$s"%2$s%3$s>%4$s</a>',
esc_url( $comments_link ),
! empty( $css_class ) ? ' class="' . $css_class . '" ' : '',
$link_attributes,
get_comments_number_text( $zero, $one, $more )
);
}
Hooks
- apply_filters( ‘comments_popup_link_attributes’, string $link_attributes )
-
Filters the comments link attributes for display.
- apply_filters( ‘respond_link’, string $respond_link, int $post_id )
-
Filters the respond link when a post has no comments.
Changelog
| Version | Description |
|---|---|
| 0.71 | Introduced. |
Skip to note 5 content
Codex
Load Different CSS classes according to Comment-condition
If you want to load different classes into
comments_popup_link(),use the following:$css_class = 'zero-comments'; $number = (int) get_comments_number( get_the_ID() ); if ( 1 === $number ) $css_class = 'one-comment'; elseif ( 1 < $number ) $css_class = 'multiple-comments'; comments_popup_link( __( 'Post a Comment', 'wpdocs_textdomain' ), __( '1 Comment', 'wpdocs_textdomain' ), __( '% Comments', 'wpdocs_textdomain' ), $css_class, __( 'Comments are Closed', 'wpdocs_textdomain' ) );Skip to note 6 content
DannyCooper
Text Response for Number of Comments with Localization
Displays the comments popup link, using “No comments yet” for no comments, “1 comment” for one, “% comments” for more than one (% replaced by # of comments), and “Comments are off for this post” if commenting is disabled. Additionally, comments-link is a custom CSS class for the link.
Skip to note 7 content
Codex
Text Response for Number of Comments
Displays the comments popup link, using “No comments yet” for no comments, “1 comment” for one, “% comments” for more than one (% replaced by # of comments), and “Comments are off for this post” if commenting is disabled. Additionally,
comments-linkis a custom CSS class for the link.<p> </p>Skip to note 8 content
Codex
Hide Comment Link When Comments Are Deactivated
Hides the paragraph element
that contains the
comments_popup_linkwhen comments are deactivated in the Write>Post screen. Good for those who want enable/disable comments post by post. Must be used in the loop.'; comments_popup_link( 'No comments yet', '1 comment', '% comments', 'comments-link', 'Comments are off for this post'); echo '</p>'; endif; ?>