Skip to content

Instantly share code, notes, and snippets.

@barbwiredmedia
Created May 15, 2014 23:30
Show Gist options
  • Select an option

  • Save barbwiredmedia/97098a075b6dfb4fbf0d to your computer and use it in GitHub Desktop.

Select an option

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/
//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');
@barbwiredmedia
Copy link
Author

No longer works WordPress 4.6+ need to revisit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment