Here are three short functions you can add to your theme’s functions.php
file to change the way excerpts work on your site.
When you use the_excerpt()
in a theme file, WordPress will first use the contents of the Excerpt field from your Post→Edit screen. If it’s empty, WordPress will auto-generate an excerpt. These first two functions change the way excerpts are generated. They will not affect any excerpts you write by hand.
Auto-generated excerpts are 55 words long. To change the length, return the number of words you prefer:
// Changing excerpt length function change_excerpt_length($length) { return 100; } add_filter('excerpt_length', 'change_excerpt_length');
Auto-generated excerpts have ‘[…]’ appended to them. To remove or change this, use:
// Changing excerpt 'more' text (normally […]) function change_excerpt_more($more) { return ' (Continue reading…)'; } add_filter('excerpt_more', 'change_excerpt_more');
In a standard WordPress setup, only posts have excerpts. If you’d like to use them for pages too, you can install a plugin (PJW Page Excerpt, Excerpt Editor) or just add this to functions.php
:
// add excerpts to pages function add_page_excerpt_meta_box() { add_meta_box( 'postexcerpt', __('Excerpt'), 'post_excerpt_meta_box', 'page', 'normal', 'core' ); } add_action( 'admin_menu', 'add_page_excerpt_meta_box' );
There is no easy function to prevent WordPress from stripping the HTML formatting from your excerpts. There are two plugins you can use, however: the_excerpt Reloaded and Advanced Excerpt.
Matt says
Is it also possible to show a “Continue Reading…” Link even when the_excerpt is modified in the backend? You know in WordPress 2.9 you have the option to set an excerpt text right underneath the normal post-text. If I do that, the “Continue Reading…” Link isn’t visible. Can I change that behaviour so that this Link is also there if the excerpt is customized by myself?
nasir says
But i need to remove continue reading only on home not permanently …….
Gordon Langley says
Thanks Stephanie, I love 5 minute fixes! Thank you.
I wanted to prompt users to write an excerpt and not to use auto excerpts, this works a treat:
// Changing excerpt length
function change_excerpt_length($length) {
return 0;
}
add_filter('excerpt_length', 'change_excerpt_length');
// Changing excerpt 'more' text (normally […])
function change_excerpt_more($more) {
return 'You forgot to write an excerpt, buddy...';
}
add_filter('excerpt_more', 'change_excerpt_more');