Custom Excerpt is returning 52 characters and not 52 words

I have this custom function that get’s the manually entered excerpt and trims it down but the problem I’m having is that it is trimming the characters down to 52 and instead I want it to return the first 52 Words like the normal excerpt.

Here’s the custom that allows me to trim the manually entered excerpt

function themeTemplate_trim_excerpt( $content ) {
    return substr( strip_tags( $content ), 0, 52 );
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'template_trim_excerpt');

This is the only way I’ve been able to cut the manually entered excerpt.

How do I make it trim it so it only shows the first 52 words?

Thanks for any feedback 🙂

2 Answers
2

The function you want is wp_trim_words(), example:

function themeTemplate_trim_excerpt( $content ) {
    $more="..."; //where $more is optional
    return wp_trim_words($content, 52, $more);
}

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'template_trim_excerpt');

Leave a Comment