Public paste
Improved WordPress Excerpt
By: Ruben Marissen | Date: Feb 22 2012 12:50 | Format: PHP | Expires: never | Size: 926 B | Hits: 541

  1. <?php
  2. function improved_trim_excerpt($text) {
  3.         global $post;
  4.         if ( '' == $text ) {
  5.                 $text = get_the_content('');
  6.                 $text = apply_filters('the_content', $text);
  7.                 $text = str_replace('\]\]\>', ']]&gt;', $text);
  8.                 $text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
  9.                 $text = strip_tags($text, '<p>');
  10.                 $excerpt_length = 80;
  11.                 $words = explode(' ', $text, $excerpt_length + 1);
  12.                 if (count($words)> $excerpt_length) {
  13.                         array_pop($words);
  14.                         array_push($words, '[...]');
  15.                         $text = implode(' ', $words);
  16.                 }
  17.         }
  18.         return $text;
  19. }
  20. remove_filter('get_the_excerpt', 'wp_trim_excerpt');
  21. add_filter('get_the_excerpt', 'improved_trim_excerpt');
  22. ?>