comment_excerpt()
云策文档标注
概述
comment_excerpt() 函数用于显示当前评论的摘要。它接受一个可选的评论 ID 或 WP_Comment 对象作为参数,默认处理当前评论。
关键要点
- 函数原型:comment_excerpt( $comment_id = 0 ),参数 $comment_id 可选,可以是评论 ID 或 WP_Comment 对象,默认值为 0 表示当前评论。
- 内部流程:通过 get_comment() 获取评论数据,然后使用 get_comment_excerpt() 检索评论摘要,最后通过 apply_filters() 应用 'comment_excerpt' 过滤器并输出结果。
- 过滤器:'comment_excerpt' 过滤器允许在显示前修改评论摘要文本,参数包括 $comment_excerpt 和 $comment_id。
- 相关函数:包括 get_comment_excerpt()、apply_filters() 和 get_comment(),用于支持评论摘要的获取和处理。
- 变更历史:从 4.4.0 版本起,$comment_id 参数支持 WP_Comment 对象;函数自 1.2.0 版本引入。
原文内容
Displays the excerpt of the current comment.
Parameters
$comment_idint|WP_Commentoptional-
WP_Comment or ID of the comment for which to print the excerpt.
Default current comment.
Source
function comment_excerpt( $comment_id = 0 ) {
$comment = get_comment( $comment_id );
$comment_excerpt = get_comment_excerpt( $comment );
/**
* Filters the comment excerpt for display.
*
* @since 1.2.0
* @since 4.1.0 The `$comment_id` parameter was added.
*
* @param string $comment_excerpt The comment excerpt text.
* @param string $comment_id The comment ID as a numeric string.
*/
echo apply_filters( 'comment_excerpt', $comment_excerpt, $comment->comment_ID );
}
Hooks
- apply_filters( ‘comment_excerpt’, string $comment_excerpt, string $comment_id )
-
Filters the comment excerpt for display.
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Added the ability for $comment_id to also accept a WP_Comment object. |
| 1.2.0 | Introduced. |
Skip to note 2 content
Codex
Example
<p>Latest comment: </p>