Change WordPress Excerpt Length Globally or by Post Type

Change the WordPress excerpt length with the excerpt_length filter. It controls the maximum length of automatically generated excerpts; WordPress normally starts with 55 words. You can use one limit for your site or choose a different limit for each custom post type.

This tutorial covers both approaches, custom Read More text, and direct trimming with wp_trim_words(). A manually entered excerpt or a theme’s own trimming function may follow different rules.

Choose the Right Excerpt Method

  • One automatic length: use the first excerpt_length example.
  • Different lengths by post type: use the second example instead of the first.
  • Custom ending: add the separate excerpt_more example when you need a Read More link.
  • A custom field or string: call wp_trim_words() directly where that text is displayed.

What is the Excerpt in WordPress?

In WordPress, an excerpt is a short description or summary of a post that’s shown to readers to grab their attention and make them want to read further. A title alone often isn’t enough to convey what an article is actually about. Excerpts are used to display a post summary on the blog page, search results page, the homepage, and in widgets wherever your posts are listed. Use the the_excerpt() function in a template to display a post’s excerpt.

Automatic vs. Manual Excerpts

WordPress generates an excerpt automatically by trimming post_content to a word count, stripping HTML tags and shortcodes in the process — that’s the “automatic” excerpt this article’s code customizes. If an author fills in the dedicated Excerpt field in the post editor instead, WordPress uses that text verbatim and completely ignores the word-count trimming logic.

Note: This is exactly why the excerpt_length filter shown below has no effect on posts with a manually written excerpt — there’s no trimming happening for WordPress to intercept. If you need a fixed length regardless of manual excerpts too, you’d need to additionally hook get_the_excerpt and run wp_trim_words() on the result yourself.

How to Change the Excerpt Length

Test this on staging and add the following filter to a child theme’s functions.php or a small site-specific plugin. If you paste it into an existing PHP section, omit the example’s opening and closing PHP tags. Do not install both length examples: they declare the same function name and would cause a redeclaration error.

<?php
/**
* WP Custom Excerpt Length Function
* Place in functions.php
* This example returns ten words, then [...]
* Manual excerpts will override this
*/
function wp_custom_excerpt_length( $length ) {
    return 10;
}
add_filter( 'excerpt_length', 'wp_custom_excerpt_length', 999 );
?>

Note: The priority 999 here isn’t arbitrary — some themes and plugins also hook into excerpt_length, and whichever callback runs last wins. Using a very high priority number is a common, if slightly blunt, way to make sure your value is the one that actually takes effect. If your length change doesn’t seem to apply, another callback running at an even higher priority is the first thing to check.

Different Excerpt Lengths per Post Type

Alternative to the previous snippet: Replace the first length function and its hook with this version; do not append it below the first one.

You might want to display a different excerpt length depending on the post type — a shorter teaser for a portfolio grid, a longer summary for blog posts. Use get_post_type() inside the filter callback to branch by post type:

<?php
/**
* WP Custom Excerpt Length Function
* Place in functions.php
* Returns a different excerpt length depending on post type
* Manual excerpts will override this
*/
function wp_custom_excerpt_length( $length ) {
    $post_type = get_post_type();

    if ( $post_type === 'portfolio' ) {
        return 15;
    }

    return 25;
}
add_filter( 'excerpt_length', 'wp_custom_excerpt_length', 999 );
?>

Context matters: With no argument, get_post_type() uses the current global post. This example is intended for a correctly established post loop. If you generate excerpts for explicit post IDs outside that loop, do not assume the filter will identify the intended post type. Replace portfolio with your registered post type identifier; our custom post type tutorial explains how those identifiers are registered.

Customizing the “Read More” Text

For core-generated automatic excerpts, WordPress normally appends […] when the text exceeds the limit. Themes and plugins can change that ending. Use the excerpt_more filter to replace it with something more useful, like an actual “Read More” link:

<?php
function wpwebguru_excerpt_more( $more ) {
    return '… <a href="' . esc_url( get_permalink() ) . '" class="read-more">' . esc_html__( 'Read More', 'wpwebguru' ) . '</a>';
}
add_filter( 'excerpt_more', 'wpwebguru_excerpt_more' );
?>

Trimming a Custom String to a Word Count Directly

If you need excerpt-style trimming somewhere other than the standard the_excerpt() call — say, trimming a custom field’s text in a template — use WordPress’s own wp_trim_words() function directly instead of reinventing word-counting logic:

<?php
echo esc_html( wp_trim_words( get_post_meta( get_the_ID(), 'subtitle', true ), 12, '…' ) );
?>

Output safety: This example displays plain text, so esc_html() escapes the trimmed value for HTML output. Trimming text does not replace context-appropriate escaping. See our custom fields tutorial for storing and displaying post metadata.

Why Your Excerpt Length Change May Not Work

  • A manual excerpt is present. Test with a post whose Excerpt field is empty before changing the filter.
  • The theme or block has its own length setting. Check whether it calls the normal excerpt functions or trims content separately.
  • A later filter overrides the value. Check other callbacks and their priorities rather than adding duplicate functions.
  • The original text is shorter than the limit. The filter sets a maximum; it does not add words or force an ending onto short text.
  • The page is cached. Clear the affected page cache, then compare a long automatic excerpt, a short one, and a manually entered excerpt.

Common Mistakes to Avoid

  • Expecting excerpt_length to affect manually written excerpts — it only affects automatically generated ones, as covered above.
  • Using a low filter priority when a theme or plugin already hooks the same filter, causing your value to be silently overridden.
  • Forgetting excerpt_more runs on the automatic excerpt only — manual excerpts won’t get your custom “Read More” text appended either, for the same reason as above.

Wrapping Up

Changing the WordPress excerpt length is a one-filter change, but getting per-post-type behavior and a proper “Read More” link right takes the few extra pieces covered above. If you’re displaying excerpts pulled through a JavaScript front end rather than a PHP template, the same wp_trim_words() approach works well inside a custom REST API endpoint that returns a trimmed summary field alongside the full content.

Leave a Reply

Your email address will not be published. Required fields are marked *

The link has been Copied to clipboard!