Advertisements

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

Advertisements

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: