函数文档

wp_trim_excerpt()

💡 云策文档标注

概述

wp_trim_excerpt() 函数用于生成文章摘要,当未提供摘要时,它会从文章内容中提取并修剪。默认限制为55个单词,可通过过滤器自定义长度和更多链接字符串。

关键要点

  • 函数返回字符串类型的摘要,如果 $text 参数为空,则从 $post 参数指定的文章中生成摘要。
  • 生成摘要时,会移除短代码、块内容和脚注,并临时解除 the_content 过滤器以避免标签处理问题。
  • 摘要长度可通过 excerpt_length 过滤器修改,默认55个单词;更多链接字符串可通过 excerpt_more 过滤器修改,默认为 ' […]'。
  • 最终结果通过 wp_trim_excerpt 过滤器应用,允许开发者进一步调整修剪后的摘要。
  • 相关函数包括 wp_trim_words()、strip_shortcodes() 和 get_the_content() 等,用于支持摘要生成过程。

代码示例

// 如果已有摘要,直接修剪
echo wp_trim_excerpt($excerpt);

// 如果无摘要,从指定文章ID生成
echo wp_trim_excerpt("", $id);

注意事项

  • 在生成摘要时,函数会临时移除 wp_filter_content_tags 和 do_blocks 过滤器,以避免在摘要中处理图像标签和块渲染,这可能导致图像计数逻辑问题,需注意恢复。
  • 参数 $post 可以是 WP_Post 实例、文章ID或对象,默认为 null,函数内部会使用 get_post() 获取文章数据。
  • 自 WordPress 6.3.0 起,函数会移除脚注标记;5.2.0 版本添加了 $post 参数。

📄 原文内容

Generates an excerpt from the content, if needed.

Description

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

The 55-word limit can be modified by plugins/themes using the ‘excerpt_length’ filter The ‘ […]’ string can be modified by plugins/themes using the ‘excerpt_more’ filter

Parameters

$textstringoptional
The excerpt. If set to empty, an excerpt is generated.
$postWP_Post|object|intoptional
WP_Post instance or Post ID/object.

Default:null

Return

string The excerpt.

Source

function wp_trim_excerpt( $text = '', $post = null ) {
	$raw_excerpt = $text;

	if ( '' === trim( $text ) ) {
		$post = get_post( $post );
		$text = get_the_content( '', false, $post );

		$text = strip_shortcodes( $text );
		$text = excerpt_remove_blocks( $text );
		$text = excerpt_remove_footnotes( $text );

		/*
		 * Temporarily unhook wp_filter_content_tags() since any tags
		 * within the excerpt are stripped out. Modifying the tags here
		 * is wasteful and can lead to bugs in the image counting logic.
		 */
		$filter_image_removed = remove_filter( 'the_content', 'wp_filter_content_tags', 12 );

		/*
		 * Temporarily unhook do_blocks() since excerpt_remove_blocks( $text )
		 * handles block rendering needed for excerpt.
		 */
		$filter_block_removed = remove_filter( 'the_content', 'do_blocks', 9 );

		/** This filter is documented in wp-includes/post-template.php */
		$text = apply_filters( 'the_content', $text );
		$text = str_replace( ']]>', ']]>', $text );

		// Restore the original filter if removed.
		if ( $filter_block_removed ) {
			add_filter( 'the_content', 'do_blocks', 9 );
		}

		/*
		 * Only restore the filter callback if it was removed above. The logic
		 * to unhook and restore only applies on the default priority of 10,
		 * which is generally used for the filter callback in WordPress core.
		 */
		if ( $filter_image_removed ) {
			add_filter( 'the_content', 'wp_filter_content_tags', 12 );
		}

		/* translators: Maximum number of words used in a post excerpt. */
		$excerpt_length = (int) _x( '55', 'excerpt_length' );

		/**
		 * Filters the maximum number of words in a post excerpt.
		 *
		 * @since 2.7.0
		 *
		 * @param int $number The maximum number of words. Default 55.
		 */
		$excerpt_length = (int) apply_filters( 'excerpt_length', $excerpt_length );

		/**
		 * Filters the string in the "more" link displayed after a trimmed excerpt.
		 *
		 * @since 2.9.0
		 *
		 * @param string $more_string The string shown within the more link.
		 */
		$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' );
		$text         = wp_trim_words( $text, $excerpt_length, $excerpt_more );

	}

	/**
	 * Filters the trimmed excerpt string.
	 *
	 * @since 2.8.0
	 *
	 * @param string $text        The trimmed text.
	 * @param string $raw_excerpt The text prior to trimming.
	 */
	return apply_filters( 'wp_trim_excerpt', $text, $raw_excerpt );
}

Hooks

apply_filters( ‘excerpt_length’, int $number )

Filters the maximum number of words in a post excerpt.

apply_filters( ‘excerpt_more’, string $more_string )

Filters the string in the “more” link displayed after a trimmed excerpt.

apply_filters( ‘the_content’, string $content )

Filters the post content.

apply_filters( ‘wp_trim_excerpt’, string $text, string $raw_excerpt )

Filters the trimmed excerpt string.

Changelog

Version Description
6.3.0 Removes footnotes markup from the excerpt content.
5.2.0 Added the $post parameter.
1.5.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    If we had excerpt already and just want to trim it into smaller amount, use this:

    echo "Here is short info about post: n" . wp_trim_excerpt($excerpt);

    and if not, you just need to use code like this, note the $id is id of the post

    echo "Here is short info about post: n" . wp_trim_excerpt("",$id);