钩子文档

wp_trim_excerpt

💡 云策文档标注

概述

wp_trim_excerpt 是一个 WordPress 过滤器钩子,用于过滤经过修剪的摘要字符串。它允许开发者在生成或修改摘要时介入处理,例如控制自动生成摘要的行为。

关键要点

  • wp_trim_excerpt 是一个过滤器钩子,用于修改或控制修剪后的摘要文本。
  • 它接受两个参数:$text(修剪后的文本)和 $raw_excerpt(修剪前的原始文本)。
  • 开发者可以通过 add_filter 添加自定义函数来干预摘要生成逻辑,例如禁用自动生成摘要。

代码示例

add_filter('wp_trim_excerpt', 'disable_auto_excerpt', 10, 2);

function disable_auto_excerpt($text, $raw_excerpt) {
    if ($raw_excerpt) {
        return $text;
    } else {
        return '';
    }
}

注意事项

  • 此钩子自 WordPress 2.8.0 版本引入,用于在 wp_trim_excerpt() 函数中应用过滤器。
  • 相关函数 wp_trim_excerpt() 位于 wp-includes/formatting.php 文件中,负责在需要时从内容生成摘要。

📄 原文内容

Filters the trimmed excerpt string.

Parameters

$textstring
The trimmed text.
$raw_excerptstring
The text prior to trimming.

Source

return apply_filters( 'wp_trim_excerpt', $text, $raw_excerpt );

Changelog

Version Description
2.8.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    This disables the attempt to return an automatically generated excerpt in the scenario where one isn’t manually set.

    add_filter('wp_trim_excerpt', 'disable_auto_excerpt', 10, 2 );
    
    // Return only the raw excerpt text if one is manually set.
      function disable_auto_excerpt($text, $raw_excerpt) {
          if ($raw_excerpt) {
            return $text;
          }
          else {
            return '';
          }
      }