Changing the WordPress Excerpt Length

A recent visitor to the WordPress Support Forum asked if is possible to increase the length of excerpts when using the the_excerpt tag. Yes it is. The default value is set to 55. The file that controls this is located at wp-includes/formatting.php. Look for excerpt_length and adjust accordingly.

Having said that altering core files is never a good idea unless you know what you're doing. A better option to achieve the result is to insert the following in to your theme's function.php file (if your theme doesn't have one then create it),

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'custom_trim_excerpt');

function custom_trim_excerpt($text) { // Fakes an excerpt if needed
global $post;
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text);
$excerpt_length = x;
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, '...');
$text = implode(' ', $words);
}
}
return $text;
}

An even easier method is to simply use a plugin from the WordPress Plugin Directory. I haven't carefully looked through the selection for a plugin that would do this but I'm sure there is at least one.

post
About Len Kutchma

Len has been blogging for over 10 years and is a rabid WordPress fan. In addition to blogging here you can find him writing the occasional article and toiling away in the forums at WeblogToolsCollection.com. He also hangs out at the WordPress support forums lending a hand where he can. Be sure to follow @wpcanada on Twitter.

Comments

  1. James Tinker says:

    Where exactly do you insert the text? I'v just tried inserting it before the last function. Makes no difference.

  2. Len Kutchma says:

    Hi James,

    What exactly is the problem you are having?

Please Note: WPCanada is a moderated community. Please read the Comment Policy.

What Do You Think?

*