2

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
    Apr 2 '14 at 0:38
  • No. No custom excerpt.
    – bodesam
    Apr 2 '14 at 15:58
4

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
    Apr 2 '14 at 16:03
0

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!

New contributor
Mahmood is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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