3

my objective is to define a custom excerpt length. I know that I can do it defining a new function in my functions.php located in the child theme of the Twenty Eleven.

It should override these lines :

/**
 * Sets the post excerpt length to 40 words.
 *
 * To override this length in a child theme, remove the filter and add your own
 * function tied to the excerpt_length filter hook.
 */
function twentyeleven_excerpt_length( $length ) {
    return 40;
}
add_filter( 'excerpt_length', 'twentyeleven_excerpt_length' );

By :

function new_excerpt_length($length) {
    return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');

But it does nothing. Even if I change the default value 40 by another.

Any idea?

4 Answers 4

2

Well I finally solved the problem. The excerpt_length function is NOT use with the excerpt box of the WordPress editor. You have to put all your content in the main area.

1
  • This drove me up the wall, so illogical!
    – Osu
    Apr 25, 2012 at 6:59
2

Also dont forget to remove the twenty eleven filter:

remove_filter( 'excerpt_length', 'twentyeleven_excerpt_length' ); 
add_filter('excerpt_length', 'new_excerpt_length');
function new_excerpt_length($length) {
  return 20;
}
1

Try this technique it will surely helpful for this type of requirement thankyou

<?php echo wp_trim_words( get_the_excerpt(), 25, '...'); ?>

2
  • Did you forget to add the technique ? Jun 10, 2020 at 20:05
  • Sorry for that here it is <?php echo wp_trim_words( get_the_excerpt(), 25, '...'); ?> Jun 10, 2020 at 20:16
0

This allows you to set the length for each tag individually.

// nouveller.com/quick-tips/quick-tip-8-limit-the-length-of-the_excerpt-in-wordpress/
function custom_excerpt_length($string, $word_limit) {
    // creates an array of words from $string (this will be our excerpt)
    // explode divides the excerpt up by using a space character
    $words = explode(' ', $string);
    // this next bit chops the $words array and sticks it back together
    // starting at the first word '0' and ending at the $word_limit
    // the $word_limit which is passed in the function will be the number
    // of words we want to use
    // implode glues the chopped up array back together using a space character
    return implode(' ', array_slice($words, 0, $word_limit));
}
function custom_excerpt_more($more) {
    global $post;
    return get_permalink($post->ID);
}
add_filter('excerpt_more', 'custom_excerpt_more');

// Call Function Like This:
// echo custom_excerpt_length(get_the_excerpt(), '25');

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.