the_excerpt() in WordPress is a core function, always used within the loop, which displays the first 55 words of the current post with [...] added at the end. While this is useful in many circumstances, it’s also very limited as many people either wish to change the number of words it displays or remove the [...] addition, or alternatively enable HTML which the_excerpt() strips out.
Previously the only way to get around this was to use the_content(‘..Read More”); which would enable you to display your own custom ‘read more’ link. But this still doesn’t solve the other problems, and the link is always dofollow.
I finally found a way to edit this by editing wordpress/wp-includes/formatting.php (starting at line 1312):
[sourcecode language='php']
/**
* Generates an excerpt from the content, if needed.
*
* The excerpt word amount will be 55 words and if the amount is greater than
* that, then the string ‘[...]‘ will be appended to the excerpt. If the string
* is less than 55 words, then the content will be returned as is.
*
* @since 1.5.0
*
* @param string $text The exerpt. If set to empty an excerpt is generated.
* @return string The excerpt.
*/
function wp_trim_excerpt($text) {
if ( ” == $text ) {
$text = get_the_content(”);
$text = strip_shortcodes( $text );
$text = apply_filters(‘the_content’, $text);
$text = str_replace(‘]]>’, ‘]]>’, $text);
$text = strip_tags($text);
$excerpt_length = apply_filters(‘excerpt_length’, 55);
$words = explode(‘ ‘, $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, ‘[...]‘);
$text = implode(‘ ‘, $words);
}
}
return $text;
}
[/sourcecode]
Where $excerpt_length = apply_filters(‘excerpt_length’, 55); and array_push($words, ‘[...]‘); control the length and read more text respectively.
Unfortunately this meant that each time a new version of WordPress was released I’d have to edit formatting.php again. Eventually I designed a plugin which solves all of these problems and means I don’t need to play around with any files or change anything after a new release.
The Custom Excerpts plugin is plug n play and allows you to set the excerpt length and link text as well as specifying HTML tags which should be allowed and whether or not the link should be nofollow or not. I actually created the plugin for myself to save me updating files with every new release, but I figured sharing it with the world wouldn’t hurt.
You can download Custom Excerpts here.
You can discuss this article and see what others are saying about it in the WordPress Discussion Forum
Did you find this article useful? Why not buy Sean a coffee?

18 comments ↓
There is another plugin with the same (?) functions: Advanced Excerpt.
Great plugin, just what I was looking for. I uncovered an issue that comes up when you leave the plugin’s “Excerpt text” field blank. When it is left blank, the plugin also ignores any length specified. This is because of the last few lines on the ce_excerpt_filter function. The array_pop and implode commands need to be outside of the last if statement.
array_pop($words);
if ($MoreText) {
array_push($words, ''.$MoreText.'');
}
$text = implode(' ', $words);
return $text;
cracking Sean, thank you
…and only 1/4 the weight of Advanced Excerpt, and easier to use
… used to good effect on my site…hope you like that, just click on Blog on the homepage to see.
Sean – I would like to have a side bar in my html website that says “Latest Blog Entries” and then list the latest five entries, and then when a visitors clicks on a link, have it go to that post in the blog.
I do not know how to code this. Can you recommend what I need? Thank you.
Bill
hmmn, sorry Sean but I changed my mind…
I was getting dozens of validation errors with this plugin, but none with advanced excerpt.
I’d prefer to use this cos it’s lighter on pageload … can you get it to validate?
I installed Custom Excerpts and set the length to 60 and test posted a long entry but it didn’t work. The entire post, rather than the excerpt, shows. I changed some settings and made a few more posts but none of them were shortened to show just the excerpt. I’m on WP 2.7. Am I missing something? Thanks.
so does this plug in support 2.7? I can’t understand why finding something to merely control the number of words displayed is so hard. I have gone through 3 plugin and none work on 2.7. And based on the last comment this one may not – please advise as it would be great to get this working.
I tried this plugin in found it very nice!
But I have one problem, I want excerpt in different length for 3 places. One in the “headline”, one in the “categories” (in the middle of the main page), and the last one in a widget.
Is there any way to do this without get rid of all of the html codes? (Now I am using a code to trim the excerpt, but it results in plain text)
Thanks for your help.
Trackbacks
Leave a Comment