Advertisement
alchymyth

custom excerpt with filters

Aug 27th, 2011
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. function improved_trim_excerpt($text) {
  2.  
  3. global $post;
  4.  
  5. if ( '' == $text ) {
  6.  
  7. $text = get_the_content('');
  8.  
  9. $text = apply_filters('the_content', $text);
  10.  
  11. $text = str_replace('\]\]\>', ']]>', $text);
  12.  
  13. $text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
  14.  
  15. $text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
  16.  
  17. $text = strip_tags($text, '<p>, <strong>, <a>, <br>');
  18.  
  19. //$excerpt_length = 55;
  20. //re-introduce the possibility to change excerpt length with filter
  21. $excerpt_length = apply_filters('excerpt_length', 55);
  22.  
  23. //re-introduce the possibility to use excerpt more filter
  24. $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
  25.  
  26. $words = explode(' ', $text, $excerpt_length + 1);
  27.  
  28. if (count($words)> $excerpt_length) {
  29.  
  30. array_pop($words);
  31.  
  32. array_push($words, $excerpt_more); //edit
  33.  
  34. $text = implode(' ', $words);
  35.  
  36. }
  37.  
  38. }
  39.  
  40. return $text;
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement