How to change WP excerpt length

Ebooks - Tips, How to — By on March 6, 2009 at 7:12 pm

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.

Tokokoo 300x250
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Random Articles

0 Comments

You can be the first one to leave a comment.

Leave a Comment


Tags: ,