Question

So I'm trying to change the standard post excerpt length.

Every single blog post on the web tells me to do it with a filter like this:

add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
function custom_excerpt_length( $length ) {
return 15; // Length of the excerpt in number of words
}

Problem is there is no change to the excerpt length. All the text is being output and not just 15 words. I have no idea why this doesn't work when it's supposed to be the solution.

I'm using <?php the_excerpt(); ?> in the post loop on home.php, category.php and tag.php to output the excerpt.

Was it helpful?

Solution

For anyone wondering, as custom excerpts don't get trimmed by the excerpt_length filter hook, try adding this filter:

function trim_custom_excerpt($excerpt) {
    if (has_excerpt()) {
        $excerpt = wp_trim_words(get_the_excerpt(), apply_filters("excerpt_length", 55));
    }

    return $excerpt;
}

add_filter("the_excerpt", "trim_custom_excerpt", 999);
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top