Preserve HTML in Excerpt
-
I’ve been playing with several different code options that people have provided to try and preserve the HTML in some excerpts on my website. If you look at my development site https://PeterCruikshank.com/dev/ and look at the bottom left Widget you can see that I can get most of the HTML formatting to display, however, there is an “unwanted return” before and after each Link and at the beginning of the excerpt, “The Novella,” is displayed as a link even though it is NOT included within the Link for “Ashes of the Dragon”.
If anyone could help me figure out how to fix the below code OR provide alternate code that would preserve the HTML without the problems I’m currently facing, I would GREATLY appreciate it.
// Preserve HTML in Excerpt function custom_wp_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); $excerpt_length = apply_filters('excerpt_length', 80); $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; $text = force_balance_tags( $text ); } else { $text = implode(' ', $words); } } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); } remove_filter('get_the_excerpt', 'wp_trim_excerpt'); add_filter('get_the_excerpt', 'custom_wp_trim_excerpt');
- The topic ‘Preserve HTML in Excerpt’ is closed to new replies.