Multiple excerpt lengths in wordpress

Multiple excerpt lengths in wordpress

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why the_excerpt() Cannot Handle Multiple Lengths and How to Fix It

WordPress does not support multiple excerpt lengths directly. The the_excerpt() function is designed around a single, global setting for length management, meaning you cannot simply pass a string key like 'length-short' or 'length-long' to the function to change its behavior dynamically within a template file.

The architecture of the core excerpt system relies on two specific filters: excerpt_more and excerpt_length. The excerpt_length filter receives an integer representing the desired word count, but it does not accept a context identifier or a custom parameter to differentiate between sidebar widgets and main content areas. Consequently, attempting to implement logic like the_excerpt('length-short') will result in the function ignoring the argument entirely and falling back to your default settings.

To achieve distinct excerpt lengths for different parts of your site—such as short summaries for sidebars, medium ones for featured grids, and full text for main articles—you must bypass the default behavior of the_excerpt() and implement a custom solution in your theme's functions.php file or a custom plugin.

Implementing Context-Aware Excerpts

The most robust approach is to create a custom function that accepts an optional length parameter. You can register this function as a callback for the excerpt_length filter, ensuring it only applies when a specific context is requested or when you decide to override the default entirely.

Here is a pattern you can use in your theme's functions.php:

function custom_excerpt_handler( $length ) {
    // Check if a custom length was passed via a global variable or query var
    if ( isset( $_GET['excerpt_len'] ) && ! empty( $_GET['excerpt_len'] ) ) {
        $length = sanitize_text_int( $_GET['excerpt_len'] );
    }

    return max( 15, absint( $length ) );
}
add_filter( 'excerpt_length', 'custom_excerpt_handler' );

In this implementation, the custom_excerpt_handler function checks for a query variable (e.g., ?excerpt_len=40). If present, it returns that value; otherwise, it defaults to 15 words. This allows you to call your custom excerpt function in templates like so:

<?php 
    global $wp_query;
    // Pass context-specific length via query args or a custom variable
    add_query_arg( 'excerpt_len', 40 ); 
    the_excerpt();
?>

However, relying on query arguments for every loop is cumbersome and can cause conflicts with pagination. A cleaner separation of concerns involves creating dedicated functions for each context.

Dedicated Functions for Specific Contexts

Instead of trying to parse parameters into the_excerpt(), define specific functions that apply the correct length filter before calling the standard excerpt logic. This keeps your template files clean and explicit about intent.

function get_short_excerpt( $more = '' ) {
    return wp_trim_words( get_the_content(), 15, $more );
}

function get_medium_excerpt( $more = '' ) {
    return wp_trim_words( get_the_content(), 40, $more );
}

function get_full_excerpt() {
    return get_the_content();
}

By using wp_trim_words() directly, you gain precise control over the output without relying on the global the_excerpt filter chain. This method is often preferred in modern development because it avoids side effects where setting a length for one loop might inadvertently affect another if filters are not scoped correctly.

Handling "The More" Text

When adjusting the word count, you must also consider the "more..." text appended by WordPress. The the_excerpt() function automatically adds a string defined in _wp_more_text (usually "Read more...") after reaching the character or word limit. If you use wp_trim_words(), this automatic appending does not happen unless you explicitly include it or configure your own "more" text.

If you require the standard "Read more..." link behavior, you should use get_the_content(false) to bypass the excerpt generation and handle trimming manually, then append the link using the_get_more() or a custom function. This ensures consistency across different length requirements while maintaining the ability for users to read the full content via the link.

Trade-offs and Best Practices

The primary trade-off here is performance versus flexibility. Using filters on every post loop adds overhead to the request lifecycle, as WordPress must execute your callback functions even if you never intend to use the modified excerpt in that specific instance.

For high-traffic sites, it is better to define these lengths once during initialization and store them in a global array or use object-oriented patterns within your theme class. Avoid scattering add_filter calls inside template files unless absolutely necessary, as this makes debugging difficult.

Furthermore, be aware that changing excerpt lengths affects SEO if the snippets displayed in search engines (SERPs) are pulled from these modified excerpts rather than the full content. While WordPress generally uses the first paragraph for SERPs, customizing excerpts can sometimes lead to duplicate content issues if not managed carefully. Always ensure that the "Read more" link points to a canonical version of the article to maintain SEO integrity.

If you need a highly scalable solution where you want to manage these lengths centrally without duplicating code in every template file, consider leveraging a plugin architecture or integrating with a headless CMS approach like Laravel, which offers more granular control over content rendering and excerpt generation logic outside of the rigid WordPress core filters.

Our network