函数文档

get_comment_excerpt()

💡 云策文档标注

概述

get_comment_excerpt() 函数用于获取指定评论的摘要,默认截取最多20个单词并添加省略号。它支持通过过滤器自定义摘要长度和内容,并处理密码保护评论。

关键要点

  • 函数返回评论的摘要字符串,可能被截断
  • 参数 $comment_id 可选,可以是评论 ID 或 WP_Comment 对象,默认为当前评论
  • 使用 wp_trim_words() 进行单词截取,默认长度为20个单词
  • 如果评论所属文章需要密码,则返回“Password protected”
  • 提供两个过滤器:comment_excerpt_length 用于修改摘要长度,get_comment_excerpt 用于修改检索到的摘要

代码示例

function get_comment_excerpt( $comment_id = 0 ) {
    $comment = get_comment( $comment_id );

    if ( ! post_password_required( $comment->comment_post_ID ) ) {
        $comment_text = strip_tags( str_replace( array( "n", "r" ), ' ', $comment->comment_content ) );
    } else {
        $comment_text = __( 'Password protected' );
    }

    /* translators: Maximum number of words used in a comment excerpt. */
    $comment_excerpt_length = (int) _x( '20', 'comment_excerpt_length' );

    $comment_excerpt_length = apply_filters( 'comment_excerpt_length', $comment_excerpt_length );

    $comment_excerpt = wp_trim_words( $comment_text, $comment_excerpt_length, '…' );

    return apply_filters( 'get_comment_excerpt', $comment_excerpt, $comment->comment_ID, $comment );
}

注意事项

  • 从版本4.4.0开始,$comment_id 参数可以接受 WP_Comment 对象
  • 函数内部使用 strip_tags() 去除 HTML 标签,并替换换行符为空格
  • 相关函数包括 wp_trim_words()、post_password_required()、get_comment() 等

📄 原文内容

Retrieves the excerpt of the given comment.

Description

Returns a maximum of 20 words with an ellipsis appended if necessary.

Parameters

$comment_idint|WP_Commentoptional
WP_Comment or ID of the comment for which to get the excerpt.
Default current comment.

Return

string The possibly truncated comment excerpt.

Source

function get_comment_excerpt( $comment_id = 0 ) {
	$comment = get_comment( $comment_id );

	if ( ! post_password_required( $comment->comment_post_ID ) ) {
		$comment_text = strip_tags( str_replace( array( "n", "r" ), ' ', $comment->comment_content ) );
	} else {
		$comment_text = __( 'Password protected' );
	}

	/* translators: Maximum number of words used in a comment excerpt. */
	$comment_excerpt_length = (int) _x( '20', 'comment_excerpt_length' );

	/**
	 * Filters the maximum number of words used in the comment excerpt.
	 *
	 * @since 4.4.0
	 *
	 * @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt.
	 */
	$comment_excerpt_length = apply_filters( 'comment_excerpt_length', $comment_excerpt_length );

	$comment_excerpt = wp_trim_words( $comment_text, $comment_excerpt_length, '…' );

	/**
	 * Filters the retrieved comment excerpt.
	 *
	 * @since 1.5.0
	 * @since 4.1.0 The `$comment_id` and `$comment` parameters were added.
	 *
	 * @param string     $comment_excerpt The comment excerpt text.
	 * @param string     $comment_id      The comment ID as a numeric string.
	 * @param WP_Comment $comment         The comment object.
	 */
	return apply_filters( 'get_comment_excerpt', $comment_excerpt, $comment->comment_ID, $comment );
}

Hooks

apply_filters( ‘comment_excerpt_length’, int $comment_excerpt_length )

Filters the maximum number of words used in the comment excerpt.

apply_filters( ‘get_comment_excerpt’, string $comment_excerpt, string $comment_id, WP_Comment $comment )

Filters the retrieved comment excerpt.

Changelog

Version Description
4.4.0 Added the ability for $comment_id to also accept a WP_Comment object.
1.5.0 Introduced.