wp_trim_excerpt

Generates an excerpt from the content, if needed.

Description

(string) wp_trim_excerpt( (string) $text = '' ); 

The excerpt word amount will be 55 words and if the amount is greater than that, then the string […] will be appended to the excerpt. If the string is less than 55 words, then the content will be returned as is.

The 55 word limit can be modified by plugins/themes using the filter The […] string can be modified by plugins/themes using the filter

Returns (string)

The excerpt.

Parameters (1)

0. $text — Optional. (string) => ''
The excerpt. If set to empty, an excerpt is generated.

Usage

  1. if ( !function_exists( 'wp_trim_excerpt' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // Optional. The excerpt. If set to empty, an excerpt is generated. 
  5. $text = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_trim_excerpt($text); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/formatting.php  
  1. function wp_trim_excerpt( $text = '' ) { 
  2. $raw_excerpt = $text; 
  3. if ( '' == $text ) { 
  4. $text = get_the_content(''); 
  5.  
  6. $text = strip_shortcodes( $text ); 
  7.  
  8. /** This filter is documented in wp-includes/post-template.php */ 
  9. $text = apply_filters( 'the_content', $text ); 
  10. $text = str_replace(']]>', ']]>', $text); 
  11.  
  12. /** 
  13. * Filters the number of words in an excerpt. 
  14. * @since 2.7.0 
  15. * @param int $number The number of words. Default 55. 
  16. */ 
  17. $excerpt_length = apply_filters( 'excerpt_length', 55 ); 
  18. /** 
  19. * Filters the string in the "more" link displayed after a trimmed excerpt. 
  20. * @since 2.9.0 
  21. * @param string $more_string The string shown within the more link. 
  22. */ 
  23. $excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' ); 
  24. $text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); 
  25. /** 
  26. * Filters the trimmed excerpt string. 
  27. * @since 2.8.0 
  28. * @param string $text The trimmed text. 
  29. * @param string $raw_excerpt The text prior to trimming. 
  30. */ 
  31. return apply_filters( 'wp_trim_excerpt', $text, $raw_excerpt );