This is what I added in my theme's functions.php file to enable formatting for excerpts in Wordpress (source of the tip):
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'bwp_trim_excerpt');
function bwp_trim_excerpt($text)
{
$raw_excerpt = $text;
if ( '' == $text ) {
$text = get_the_content('');
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text, '<em><strong><i><b><a><code>');
$excerpt_length = apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
$words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
if ( count($words) > $excerpt_length ) {
array_pop($words);
$text = implode(' ', $words);
$text = $text . $excerpt_more;
} else {
$text = implode(' ', $words);
}
}
return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}
The problem with enabling formatting in post excerpts seems to be that, if the text that is formatted by a tag (be it <b>, <strong>, <i>, <em>, or any other for that matter) is cut off by the excerpt in between, your whole page's formatting will be overridden by that tag. Take a look at this screenshot for instance:
I believe that’s the reason why formatting for excerpts isn’t enabled by default. Is there anyway to fix this? Is something wrong with the code?
Hope I can get some help here. Thanks!
