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

Filters the retrieved post excerpt.


Description Description


Parameters Parameters

$post_excerpt

(string) The post excerpt.

$post

(WP_Post) Post object.


Top ↑

Source Source

File: wp-includes/post-template.php

View on Trac


Top ↑

Changelog Changelog

Changelog
Version Description
4.5.0 Introduced the $post parameter.
1.2.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by AD_Taylor

    When using this filter on a string stored in a variable, the string will not be trimmed by the excerpt_length filter.

    This is expected behaviour. By passing a long string through the get_the_excerpt filter, you are simulating the case where you have entered the same long text into the Excerpt field in the Edit Post screen. When a custom excerpt is present on the post, no trimming is done (though other content filters are applied).

    If you want to simulate the automatic excerpt trimming on arbitrary text, you can pass the text to wp_trim_words() yourself, with a function like this:

    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;
    }

    The function above can be used like this:

    $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 );

You must log in before being able to contribute a note or feedback.