Created
May 15, 2014 23:30
-
-
Save barbwiredmedia/97098a075b6dfb4fbf0d to your computer and use it in GitHub Desktop.
Wordpress filter the_excerpt to include page formatting on output, trim excerpt length. From Aaron Russell: http://aaronrussell.co.uk/legacy/improving-wordpress-the_excerpt/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Excerpt with formating from WYSIWYG | |
| function improved_trim_excerpt($text) { | |
| global $post; | |
| if ( '' == $text ) { | |
| $text = get_the_content(''); | |
| $text = apply_filters('the_content', $text); | |
| $text = str_replace('\]\]\>', ']]>', $text); | |
| $text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text); | |
| //$text = strip_tags($text, '<p>'); | |
| $excerpt_length = 70; | |
| $perma = get_the_permalink(); | |
| $words = explode(' ', $text, $excerpt_length + 1); | |
| if (count($words)> $excerpt_length) { | |
| array_pop($words); | |
| array_push($words, '... <a href="' . $perma .'">Read More</a>'); | |
| $text = implode(' ', $words); | |
| } | |
| } | |
| return $text; | |
| } | |
| remove_filter('get_the_excerpt', 'wp_trim_excerpt'); | |
| add_filter('get_the_excerpt', 'improved_trim_excerpt'); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No longer works WordPress 4.6+ need to revisit