W3docs

Multiple excerpt lengths in wordpress

In WordPress, the length of the excerpt (the summary text displayed on the homepage or archive pages) can be controlled by the theme or by a plugin.

In WordPress, the length of the excerpt (the summary text displayed on the homepage or archive pages) can be controlled by the theme or by a plugin. The theme may have a specific setting for the excerpt length, or it may use the default WordPress excerpt length of 55 words. If a theme does not have a specific setting, you can use a plugin such as Advanced Excerpt to set the excerpt length. You can also customize the excerpt length by adding a function to the functions.php file of your theme.

To implement multiple excerpt lengths, hook into the excerpt_length filter and return different values based on the current page context:

function custom_excerpt_length( $length ) {
    if ( is_home() ) {
        return 20; // Shorter excerpt for homepage
    } elseif ( is_archive() ) {
        return 55; // Standard excerpt for archive pages
    }
    return $length; // Fallback to default
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

Once added, any call to the_excerpt() or get_the_excerpt() will automatically use the appropriate length based on the current template context.