Posted By

visualadvance on 02/13/12


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

adeg


Wordpress: Customized Excerpt (Length and Tags)


 / Published in: PHP
 

URL: http://aaronrussell.co.uk/legacy/improving-wordpress-the_excerpt/

This is a modified version of one from Aaron Russell. Super handy, allows you to pass the length as a parameter, and define the tags you wish to allow in the excerpt. It is tweaked to fit our needs. Tweaks include: - no need to "echo", just called customexcerpt (); - Can pass the length as a parameter customexcerpt (81);

  1. function custom_excerpt($length) {
  2.  
  3. global $post;
  4.  
  5. $text = get_the_content('');
  6. $text = apply_filters('the_content', $text);
  7. $text = str_replace('\]\]\>', ']]>', $text);
  8. $text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
  9. $text = strip_tags($text, '<p><div><h1><h2><b>');
  10. $excerpt_length = $length;
  11. $words = explode(' ', $text, $excerpt_length + 1);
  12. if (count($words)> $excerpt_length) {
  13. array_pop($words);
  14. $text = implode(' ', $words);
  15. }
  16.  
  17. echo $text.'...<a href="'. get_permalink($post->ID) . '">Continue Reading</a>';
  18. }

Report this snippet  

You need to login to post a comment.