4

I'm trying to build a theme, I want to control the length of the post excerpts by doing something like this in functions.php:

function theme_excerpt_length( $length ) {
    return 45;
}
add_filter( 'excerpt_length', 'theme_excerpt_length', 999 );

But it doesn't seem to have the desired effect of reducing length of excerpt to 45 words. Even, without the function, some excerpts are longer than the default 55 words. What could be wrong?

2
  • 4
    do those posts have a custom excerpt entered in the excerpt field?
    – Milo
    Commented Apr 2, 2014 at 0:38
  • No. No custom excerpt.
    – bodesam
    Commented Apr 2, 2014 at 15:58

4 Answers 4

5

The are two quick ways to display custom excerpt lengths in your theme using wp_trim_words. Remember, if you use the_excerpt(), your excerpt length will always be a maximum of 55, never more. If you use the_content() on the other hand, you can specify an excerpt length of more than 55 words.

Use the following to display your excerpt. Remember to replace get_the_excerpt with get_the_content if you need an excerpt of more than 55, and replace <a href="'. esc_url( get_permalink() ) . '">' . '&nbsp;&hellip;' . __( 'Read more &nbsp;&raquo;', 'pietergoosen' ) . '</a> with any excerpt ending you need. My ending display a "read more" text with the name of the post.

function pietergoosen_custom_excerpts($limit) {
    return wp_trim_words(get_the_excerpt(), $limit, '<a href="'. esc_url( get_permalink() ) . '">' . '&nbsp;&hellip;' . __( 'Read more &nbsp;&raquo;', 'pietergoosen' ) . '</a>');
}

Now just use echo pietergoosen_custom_excerpts($limit); anywhere in your templates where you need to display excerpts. Just change $limit to the actual amount of words, for example echo pietergoosen_custom_excerpts(45); to display 45 words

EDIT

Have a look at my answer on a custom excerpt as well

3

This is the function I use for controlling excerpt/content lengths, especially in situations where the end user won't likely remember to add the <!--more--> tag, but the design of the theme requires is.

function excerpt($limit) {
    $excerpt = explode(' ', get_the_excerpt(), $limit);
    if (count($excerpt)>=$limit) {
        array_pop($excerpt);
        $excerpt = implode(" ",$excerpt).'...';
      } else {
        $excerpt = implode(" ",$excerpt);
      } 
    $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
    return $excerpt;
    }

From there, you can change your excerpt code in your template files from: <?php the_excerpt();?>

...to:

<?php echo excerpt(25);?>

where 25 is the number of characters you want displayed. When designing themes, I tend to stick with characters instead of words because the spacing is a little more consistent.

1
  • 1
    I tried this. There's a reduction in length but not exactly the specified excerpt length.For instance I specified 45, i still got an excerpt like: "Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem..."
    – bodesam
    Commented Apr 2, 2014 at 16:03
1

If you have custom excerpt, the "excrept_lenght" filter will not work for it. You can use this filter to trim custom excerpt.

function custom_excerpt_length($excerpt) {
    if (has_excerpt()) {
        $excerpt = wp_trim_words(get_the_excerpt(), apply_filters("excerpt_length", 30));
    }
    return $excerpt;
}
add_filter("the_excerpt", "custom_excerpt_length", 999);

Good Luck!

1
  • Was searching for a working solution for half an hour. This one seems to work, thanks! Commented Apr 29, 2022 at 14:58
0

If you found this question, because you are using the "Excerpt" block in Gutenberg editor, and you are trying to set a default length for it (or anything other than the originally allowed 10-100 word value) you may find that the excerpt_length filter has no effect on the block, and you can't even use the_excerpt filter to override the excerpt's content. This is is because of the latest updates in the Excerpt block in WordPress.

However, you can use the following code to edit the default block settings of the Excerpt Gutenberg block:

function filter_metadata_registration( $metadata ) {
    if ($metadata['name'] === 'core/post-excerpt') {
        $metadata['attributes']['excerptLength'] = [
            'type' => 'number',
            'default' => 9999,
        ];
    }
    return $metadata;
};
add_filter( 'block_type_metadata', 'filter_metadata_registration' );

More on this: https://core.trac.wordpress.org/ticket/59348

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.