Using WP_TRIM_WORDS on the_title in WordPress

Straight from the WP Codex there is a command to trim the word count on text – commonly used for controlling the_excerpt length to customise ‘read more’ and ‘introduction’ type on a page. $trimmed = wp_trim_words( $text, $num_words = 55, $more = null );

The above example will trim a $text variable to 55 words and have nothing trailing.

Tip for new comers: Using wp_trim_words() on the_title returns an array and not a string, so it doesn’t work with.

Instead you are likely looking for:

echo wp_trim_words( get_the_title(), 55, null );

Leave a Reply