If you type your poets like this in the editor and separate them by new lines:
This is a phrase
This is another phrase
Then you can divide them by new line, and then return them to the front-end. You can do this by using the get_the_excerpt
filter. This goes in your theme’s functions.php
file:
apply_filters( 'get_the_excerpt', 'divide_by_newline' );
function divide_by_newline() {
// Get the content of the poet
$content = get_the_content();
// Let's trim the excerpt by default value of 55 words
$content = wp_trim_words( $content , 55, '...' );
// Check if it's empty
if( $content ) {
// Divide the poets by new line
$content_array = explode( "\n", $content );
// Store 5 of them into a string
foreach( $content_array as $key=>$phrase ) {
if( $key < 6 ) {
$excerpt .= '<span class="poet-excerpt">'.$phrase.'</span>';
}
}
} else {
$excerpt="";
}
// Return the data
return $excerpt;
}
Now, your excerpt’s structure will be like this:
<span class="poet-excerpt">This is a phrase</span>
<span class="poet-excerpt">This is another phrase</span>
We’re not done yet. You have to change the default view of your spans. By default, a span is an inline element, it means they will not make a new line. By using this piece of CSS, we make them behave like blocks:
.poet-excerpt{
display:block;
}
You can easily add this by heading to Appearance > Customizer > Custom CSS
without modifying your style.css
.
But I have to mention, this works well only if you have a poet. This does not work for other excerpts.
All done. Code is also poetry!