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.
Leave a Reply
You must be logged in to post a comment.
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
andexcerpt_more
) are applied and which callswp_trim_words()
, which in turn calls uponwp_strip_all_tags()
. All three functions are located in wp-includes/formatting.phpHence 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 forwp_trim_excerpt()
:Same to Johannes Pille’s solution, the bellowing solution should be more adaptive.
In detail:
Here’s full code:
wouldn’t it be simpler to apply the_content filter to the excerpt?
i only use this occasionally, right within the template file:
At the moment, it is possible to use
the_excerpt()
funtion to display the excerpt with line breaks. But if you like to return the result you can use for that purpose a function like this:It applies a filters to the excerpt using
apply_filters('the_excerpt', $excerpt )
like in the wp functionthe_excerpt()
but returns the string without displaying it.Also, if you like to allow only line breaks in the excerpt, you can add
$excerpt = strip_tags($excerpt,'<br>')
below the lineapply_filters
.Hope it helps!
also could just use PHP’s nl2br() function:
— or —