The following filter lets you set the excerpt length that WordPress displays when calling the the_excerpt function. In this example we set the excerpt length to 20 words (the default is 55):
function sv_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'sv_excerpt_length', 999 );
The filter only affects excerpts that WordPress generates automatically from the post content. A manual excerpt entered in the editor is shown in full. The priority of 999 makes sure your value wins over a theme or plugin that sets its own length.
This uses the excerpt_length filter. Learn more in WordPress Hooks Explained.