当前浏览:首页 / WordPress钩子 / get_the_excerpt

apply_filters( 'get_the_excerpt', string $post_excerpt, WP_Post $post )

过滤检索到的帖子摘要

excerpt 摘要more...

themore...


参数

$post_excerpt

(string) 文章摘录

$post

(WP_Post) Post对象


说明

get_the_excerpt过滤器用于在从数据库检索文章节选后,以及从get_the_excerpt()函数返回之前,过滤文章节选。

调用get_the_excerpt过滤器时,会向过滤器函数传递一个包含post摘录的参数。

function filter_function_name( $excerpt ) {
  # ...
}
add_filter( 'get_the_excerpt', 'filter_function_name' );

其中,“filter_function_name”是检索摘录时WordPress应调用的函数。请注意,过滤功能必须在完成处理后返回摘录,否则显示摘录的页面部分将为空,其他插件也会过滤摘录,可能会产生错误。

filter_function_name”应该是唯一的函数名。它不能与已声明的任何其他函数重名。



源码

查看源码 官方文档


更新日志

版本描述
4.5.0引入了$post参数
1.2.0开始引入

使用示例

  • 示例1

    当对存储在变量中的字符串使用此过滤器时,excerpt_length过滤器不会修剪字符串。

    这是预期的行为。通过将一个长字符串传递给get_the_excerpt过滤器,可以模拟在Edit Post屏幕的Excerpt字段中输入相同长文本的情况。当文章中出现自定义摘录时,不会进行修剪(尽管应用了其他内容过滤器)。

    如果要模拟对任意文本的自动摘录修剪,可以使用如下函数将文本传递给wp_trim_words()

    function wpcodex_format_custom_excerpt( $text ) {
      /**
       * Filters the number of words in an excerpt.
       *
       * @since 2.7.0
       *
       * @param int $number The number of words. Default 55.
       */
      $excerpt_length = apply_filters( 'excerpt_length', 55 );
      /**
       * 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 );
    
      // Format the text
      $text = apply_filters( 'get_the_excerpt', $text );
    
      return $text;
    }

    上述功能可以这样使用:

    $text = "Etiam laoreet libero sit amet sem tempor, vel dictum odio bibendum. Aenean odio ligula, placerat sodales dui non, tempor dictum lorem. Vestibulum rutrum, velit a placerat imperdiet, erat massa porta urna, a convallis diam lorem non sapien. Vivamus in risus non quam aliquet blandit nec fermentum dolor. Duis ultricies lectus eu cursus fermentum. Sed eget convallis odio. Ut ut dolor nec nisi varius blandit a eget justo. Integer sed tellus eget leo pretium ultricies. Nullam rhoncus ex sit amet dolor pellentesque feugiat. Nullam a eros orci. Etiam egestas est erat, eu pellentesque sapien dignissim vel. Nulla malesuada commodo justo, at egestas purus egestas fermentum.
    
    Vestibulum vitae metus ullamcorper, vehicula urna eu, ullamcorper leo. Sed sit amet eros eget metus bibendum blandit. Praesent ac lacinia purus. Donec laoreet tempus dui id faucibus. Nulla laoreet cursus laoreet. Nulla et arcu ex. Pellentesque cursus non metus a volutpat. Donec pretium orci et metus molestie, ac feugiat lacus auctor. Ut vehicula eu augue eu gravida. In scelerisque risus in rutrum vestibulum. Phasellus egestas augue quis enim varius, ut sagittis massa auctor.";
    $text = wpcodex_format_custom_excerpt( $text );
  • 示例2

    从Codex迁移的示例:

    自定义“更多”链接

    这个来自twentyeleven主题的示例为帖子摘录添加了一个自定义的“阅读更多”链接。参见has_excerpt()is_attachment()

    /**
     * Adds a pretty "Continue Reading" link to custom post excerpts.
     *
     * To override this link in a child theme, remove the filter and add your own
     * function tied to the get_the_excerpt filter hook.
     */
    function twentyeleven_custom_excerpt_more( $output ) {
      if ( has_excerpt() && ! is_attachment() ) {
        $output .= twentyeleven_continue_reading_link();
      }
      return $output;
    }
    add_filter( 'get_the_excerpt', 'twentyeleven_custom_excerpt_more' );