Advertisement
alchymyth

wp_trim_excerpt()

Apr 16th, 2011
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.33 KB | None | 0 0
  1. /**
  2.  * Generates an excerpt from the content, if needed.
  3.  *
  4.  * The excerpt word amount will be 55 words and if the amount is greater than
  5.  * that, then the string ' [...]' will be appended to the excerpt. If the string
  6.  * is less than 55 words, then the content will be returned as is.
  7.  *
  8.  * The 55 word limit can be modified by plugins/themes using the excerpt_length filter
  9.  * The ' [...]' string can be modified by plugins/themes using the excerpt_more filter
  10.  *
  11.  * @since 1.5.0
  12.  *
  13.  * @param string $text The excerpt. If set to empty an excerpt is generated.
  14.  * @return string The excerpt.
  15.  */
  16. function wp_trim_excerpt($text) {
  17.     $raw_excerpt = $text;
  18.     if ( '' == $text ) {
  19.         $text = get_the_content('');
  20.  
  21.         $text = strip_shortcodes( $text );
  22.  
  23.         $text = apply_filters('the_content', $text);
  24.         $text = str_replace(']]>', ']]>', $text);
  25.         $text = strip_tags($text);
  26.         $excerpt_length = apply_filters('excerpt_length', 55);
  27.         $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
  28.         $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
  29.         if ( count($words) > $excerpt_length ) {
  30.             array_pop($words);
  31.             $text = implode(' ', $words);
  32.             $text = $text . $excerpt_more;
  33.         } else {
  34.             $text = implode(' ', $words);
  35.         }
  36.     }
  37.     return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement