wp_trim_words

Definition:
function wp_trim_words( $text, $num_words = 55, $more = null ) {}

Trims text to a certain number of words.

Parameters

  • string $text: Text to trim.
  • int $num_words: Number of words. Default 55.
  • string $more: What to append if $text needs to be trimmed. Default ‘…’.

Return values

returns:Trimmed text.

Defined filters

  • wp_trim_words
    apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text )

Source code

function wp_trim_words( $text, $num_words = 55, $more = null ) {

	if ( null === $more )

		$more = __( '…' );

	$original_text = $text;

	$text = wp_strip_all_tags( $text );

	$words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );

	if ( count( $words_array ) > $num_words ) {

		array_pop( $words_array );

		$text = implode( ' ', $words_array );

		$text = $text . $more;

	} else {

		$text = implode( ' ', $words_array );

	}

	return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text );

}

17872

No comments yet... Be the first to leave a reply!

Leave a comment