Improving WordPress’ the_excerpt() template tag


/ Published in: PHP
Save to your folder(s)



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

URL: http://www.milienzo.com/2007/09/02/improving-wordpress-the_excerpt-template-tag/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.