I am using wp_trim_words to trim some excerpts on my homepage. It's working fine except that it's stripping the HTML tags from the excerpts. I need to be able to make certain pieces of the excerpt bold (using <strong>
). Following the instructions here, I tried removing the wp_trim_words function and replacing it with a new one using the following code, which replaces $text = wp_strip_all_tags( $text );
from the original WP function with $text = strip_tags($text, '<strong>',);
. But this breaks the site. What am I doing wrong?
// Remove Reverie Trim Words
function remove_trim_words() {
remove_filter('get_the_excerpt', 'wp_trim_words');
add_filter('get_the_excerpt', 'oakwood_trim_words');
}
// Replace Reverie Trim Words
function oakwood_trim_words( $text, $num_words = 55, $more = null ) {
if ( null === $more )
$more = __( '…' );
$original_text = $text;
$text = strip_tags($text, '<strong>',);
/* translators: If your word count is based on single characters (East Asian characters),
enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */
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 ) > $num_words ) {
array_pop( $words_array );
$text = implode( $sep, $words_array );
$text = $text . $more;
} else {
$text = implode( $sep, $words_array );
}
/**
* Filter the text content after words have been trimmed.
*
* @since 3.3.0
*
* @param string $text The trimmed text.
* @param int $num_words The number of words to trim the text to. Default 5.
* @param string $more An optional string to append to the end of the trimmed text, e.g. ….
* @param string $original_text The text before it was trimmed.
*/
return apply_filters( 'oakwood_trim_words', $text, $num_words, $more, $original_text );
}
COMPLETE GUIDE TO EXCERPTS
I've recently answered a few questions regarding excerpts, so I' going to give a detailed explanation covering as much as I can.
HTML TAGS/FORMATTING
the_excerpt()
first of all doesn't except any parameters, so nothing can be passed to it. In is a fact thatthe_excerpt()
trims the content to 55 words, and all html tags are stripped before returning the text.the_excerpt()
is located in wp-includes/post-template.php. To allow certain or all html tags in the excerpt, a new excerpt have to be created.First of all, the original function needs to be removed first, and then the new function needs to be hooked to
get_the_excerpt
. Please take note, this new excerpt will still be callable asthe_excerpt()
in template files, no need to change that.get_the_excerpt()
is located in wp-includes/post-template.php.The excerpt uses
wp_trim_excerpt
to return the trimmed text, so we need to removewp_trim_excerpt
first from the excerpt filter.wp_trim_excerpt()
is located in wp-includes/formatting.php, line 2355. This is how:You can now add your new excerpt to
get_the_excerpt
To allow html tags/formatting, we will need to specify which tags you will need to allow. You can use the following
strip_tags
statement to achieve thatThe second argument
wpse_allowedtags()
is a small function that is used to add the tagsthe_excerpt()
will allow. For a complete list of valid HTML5 tags, go and check it out here. Here is function, add any html tag to this that you need to allow/keepIf you need to allow all HTML tags, that is, no stripping of any tags, the
strips_tags()
function can be omitted/removed completely.A point to note however, when html tags are allowed, these tags are counted as words, so your word count for excerpts with tags and without tags will not be the same. To correct that, you will need to remove these tags from the actual word count first so that only words are counted.
I have written an excerpt that will allow all tags, count only words as words, and complete a sentence after the set amount of words (won't trim text mid-sentence) and add a read more text after the last word.
Here is the complete code
You can just remove the '//' from functions that you need extra.
CUSTOM EXCERPT LENGTHS
Sometimes you need to display simple excerpts of different lengths and it is not viable to write an excerpt for every post/function/page. Here is a nice small little function using
wp_trim_words
What this little function do is taking
get_the_excerpt
trimming that to$limit
set by the user and returning the text with a read more link at the end.You can call this excerpt as follow in your template
where
$limit
will be your word count, so an excerpt of 30 word will beJust one thing to remember here, if you set your limit to more that 55 words, only 55 words will be returned as the excerpt is only 55 words in length. If longer excerpts are needed, use
get_the_content
instead.CUSTOM EXCERPT LENGTH
If you just need to alter the length of
the_excerpt()
, you can use the following functionRemember, you will need to set a priority bigger than 10 so that your custom function executes after the default.
ADD READ MORE LINK
All text returned by the excerpt have the hated
[...]
at the end that is not clickable. To add a read more text in the place of the hellips, use this functionEDIT
Excerpt first paragraph
I want to keep this complete, so here is the excerpt that trims after the first paragraph
Here is a function that keeps HTML tags in tact, adds a "Read More" link at the end of the excerpt and trims the excerpt after the first paragraph.
You can't allow html formatting in excerpt as if the text cut's off before the tag closes it will create undesired formatting on the rest of the page. Think of bold being applied from 20th character to the 1000th character. If formatting is used it will apply the starting bold tag but not the ending on as the text limit is till 55th character only.
Try adding this to your functions.php