Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

how can I include a line-break inside the_excerpt. I’ve modified the length and the more-button via my functions.php. I’m using teaser for every blog-entry and sometimes it looks a bit ugly, that there no line-breaks included.

share|improve this question

1 Answer 1

There is no filter that would allow you to set allowable tags not to be removed by the_excerpt(). Arguably a shortcoming of the core.

Anyhow, the actual excerpt generation does not happen in that template tag but entirely elsewhere:

Excerpts are generated by the function wp_trim_excerpt(), inside of which the excerpt filters you are already using (excerpt_length and excerpt_more) are applied and which calls wp_trim_words(), which in turn calls upon wp_strip_all_tags(). All three functions are located in wp-includes/formatting.php

Hence in the absence of a filter for the case and the inevitability of your excerpt running through wp_strip_all_tags(), the only possibility to preserve some tags is adding a custom replacement function for wp_trim_excerpt():

function wpse67498_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', 55 );
        $excerpt_more = apply_filters( 'excerpt_more', ' ' . '[...]' );

        $allowable = '<br>';
        $text = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $text );
        $text = trim( strip_tags( $text, $allowable ) );

        if ( 'characters' == _x( 'words', 'word count: words or characters?' )
            && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) )
        {
            $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
            preg_match_all( '/./u', $text, $words_array );
            $words_array = array_slice( $words_array[0], 0, $num_words + 1 );
            $sep = '';
        } else {
            $words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );
            $sep = ' ';
        }

        if ( count( $words_array ) > $excerpt_length ) {
            array_pop( $words_array );
            $text = implode( $sep, $words_array );
            $text = $text . $excerpt_more;
        } else {
            $text = implode( $sep, $words_array );
        }
    }

    return apply_filters( 'wp_trim_excerpt', $text, $raw_excerpt );
}

remove_filter( 'get_the_excerpt', 'wp_trim_excerpt');
add_filter( 'get_the_excerpt', 'wpse67498_wp_trim_excerpt' );
share|improve this answer
    
Okay, thank you. But now it seems, that I can’t shorten the excerpt anymore to 20 words and put a more button instead of [...] –  Nitzki Oct 8 '12 at 16:03
    
So how can I now edit the excerpt_length and the excerpt_more? It doesn’t work any more. –  Nitzki Oct 9 '12 at 10:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.