wp_trim_words()WP 3.3.0

Trims the given text to the specified number of words.

A WordPress helper function that can be used instead of the_excerpt() to output a portion of content trimmed to the desired number of words.

Use wp_html_excerpt() when you need to truncate an HTML string to a specified number of characters.

This function is localized. For languages that count 'words' by the individual character (such as East Asian languages), the $num_words argument will apply to the number of individual characters.

Hooks from the function

Returns

String. Trimmed text.

Usage

wp_trim_words( $text, $num_words, $more );
$text(string) (required)
The text to be trimmed.
$num_words(number)
The number of words. Specifies how many words to keep from the beginning of the string.
Default: 55
$more(string)
The text that ends the string if it was truncated. By default, an ellipsis - is used to indicate that the text has been truncated.
Default: '…'

Examples

0

#1 Demo - cut text to 5 words

The example shows how the function works:

$text = '"Posner" - a weekly program by Vladimir Posner, which is broadcast since November 2008 on the nightly air of Channel One';

echo wp_trim_words( $text, 5, ' ...' );

// Output: 
// "Posner" is a weekly author's program ...
0

#2 Analog to the_excerpt()

Example, shows how to get the current post content and trim it down to 40 words and add a Read more... link at the end, if needed:

$content = get_the_content();

$trimmed_content = wp_trim_words( $content, 40, 
	sprintf( '<a href="%s"> ...Read More</a>', get_permalink() )
);

echo $trimmed_content;

Changelog

Since 3.3.0 Introduced.

wp_trim_words() code WP 6.6.2

function wp_trim_words( $text, $num_words = 55, $more = null ) {
	if ( null === $more ) {
		$more = __( '&hellip;' );
	}

	$original_text = $text;
	$text          = wp_strip_all_tags( $text );
	$num_words     = (int) $num_words;

	if ( str_starts_with( wp_get_word_count_type(), '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 );
	}

	/**
	 * Filters 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 55.
	 * @param string $more          An optional string to append to the end of the trimmed text, e.g. &hellip;.
	 * @param string $original_text The text before it was trimmed.
	 */
	return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text );
}