X

Use wp_trim_words to trim your text in wordpress

Ronneby theme demos

WordPress 3.3+ has a core function called wp_trim_words() this function will trim text to a specified number of words and return the result. Adding this snippet to your wordpress loop will let you easily trim the number of words displayed within the_content, the_excerpt, the_title or any other value you pass to this core function.



Code Snippet:


echo wp_trim_words( get_the_content(), 100 ); // post content
echo wp_trim_words( get_the_excerpt(), 100 ); // post excerpt
echo wp_trim_words( get_the_title(), 100 ); // post title


Code Snippet:


$trimmed = wp_trim_words( $text, $num_words = 55, $more = null );

Parameters

$text
(string) (required) Text to trim

Default: None
$num_words
(integer) (optional) Number of words

Default: 55
$more
(string) (optional) What to append if $text needs to be trimmed.

Default: ‘…

Comments  Leave a Reply

  1. wordpress has lots in its upgraded versions..

  2. Hello, thanks for all your useful advice. I have one question, not regarding the current post, but your webpage theme. Could you share with us (or maybe you have done so before, but I can’t find it?) how this great categories/tag listing in the sidebar is made? It looks really great and I wondered how to create a similar listing – with “progress bars” showing how many posts were publisher in a given category/tag.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!