Support » Developing with WordPress » 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 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 ) {
    $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');
Viewing 7 replies - 1 through 7 (of 7 total)
  • hey there, for your excerpt, you can use my code. here is my code

    function cuplikan($charlength) {
    	$excerpt = get_the_excerpt();
    	if ( mb_strlen( $excerpt ) > $charlength ) {
    		$subex = mb_substr( $excerpt, 0, $charlength - 5 );
    		$exwords = explode( ' ', $subex );
    		$excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
    		if ( $excut < 0 ) {
    			echo mb_substr( $subex, 0, $excut );
    		} else {
    			echo $subex;
    		echo '';
    	} else {
    		echo $excerpt;

    and cal it with

    <?php cuplikan(140);?>

    140 is your excerpt lenght

    it will give you post excerpt without html tag, and also without unwanted link. also if you want to add your post link. you can use this.
    <p><?php cuplikan(140);?>, <a href="<?php the_permalink();?>">read more</a></p>

    Moderator keesiemeijer


    The unwanted return is from the display: block; in this CSS rule in the news-min.css stylesheet file.

    .news__list a {
        text-decoration: none !important;
        border: none !important;
        box-shadow: none !important;
        display: block;
        overflow: hidden;
        outline: none;

    Try adding this additional CSS in the customizer.

    .news__list a {
        display: inline;

    Thanks Keesiemeijer, that took care of the “unwanted returns”. Great. The only other issue is the opening line “The Novella,” still displays with a Link and it shouldn’t. What it appears to be doing is picking up and continuing the Link from the title, “Ashes of the Dragon Coming Soon”. Not sure how to stop this? The article is coming from a News plugin, where I create each Article just like a Post or Page, except using a News Post Type. This is a snapshot pic of the Post page for the News Article so you can see what it looks like.

    Moderator bcworkz


    Your title links do not have a closing </a> tag. The same problem exists for the new site post above. Notice the entire post is one link. Look at the template code outputting the title.

    So you mean something in my Theme template? Not sure I understand.

    I’m not sure what post you are talking about when you say “The same problem exists for the new site post above.” Also when you say “Look at the template code outputting the title.” Unfortunately I’m not sure how to fix this.

    Moderator keesiemeijer


    It’s because the (news) plugin wraps the whole news item inside a link. Since HTML tags are now allowed in the excerpt the browser has trouble because of nested links.

    Ask the plugin author if there is a way to change the HTML produced by this plugin.

    • This reply was modified 2 years, 8 months ago by keesiemeijer. Reason: Add link to plugin
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Preserve HTML in Excerpt’ is closed to new replies.