
Sometimes it’s the simplest things that baffle those of us working with WordPress. My goal on a recent project was to grab snippets of the latest blog entries and display the text without images. That’s easy enough using the default WordPress feature, which strips out any tags including images.
< ?php the_excerpt(); ?>
Unfortunately, the_excerpt needs a little love if you want to add a nice “Read more…” link or change the number of characters displayed for each post. By default the number of characters is 55. You can search on this until you’re blue in the face because developers have hacked up various solutions to this over the years, but many of them no longer work in WordPress 3+.
No worries, though. All you need to do is add a few filters to your theme’s functions.php file, customize to your liking, and call it a day.
Change the Excerpt Length
function new_excerpt_length($length) {
return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');
20 is the number of characters in the above example, so change to whatever you’d like.
Change or Remove the Default [...] Text
function new_excerpt_more($more) {
return '[.....]';
}
add_filter('excerpt_more', 'new_excerpt_more');
If you’d like to get rid of the [...] completely, just remove the brackets and everything in between the ” marks above. You can also customize this filter to insert a nice “Read more” link at the end of your excerpt.
Add a Read More Link to the Excerpt
function new_excerpt_more($more) {
global $post;
return '... <a href="'. get_permalink($post->ID) . '">Read the Rest...</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');
Customize away!














