Editing The Number Of Words For The Excerpt
Posted by Nile | Posted in Tutorials | Posted on 11-06-2009 | Comments Off
Tags: excerpt
Some people might think it is the number of lines you have to define to edit how long or short your excerpt in your WordPress article. It is not. It is defined by how many words. WordPress by default picks the first 55 words as your excerpt. However, if you wish to edit to make it shorter or longer:
EDIT: Please note, this way changes the core files and upgrades will only change it back. (Thanks Mike Little!)
1. Go to your wp-includes folder and find the formatting.php file
2. Look for the group of code like
/**
* Generates an excerpt from the content, if needed.
*
* The excerpt word amount will be 55 words and if the amount is greater than
* that, then the string '[...]' will be appended to the excerpt. If the string
* is less than 55 words, then the content will be returned as is.
*
* @since 1.5.0
*
* @param string $text The exerpt. If set to empty an excerpt is generated.
* @return string The excerpt.
*/
function wp_trim_excerpt($text) {
if ( '' == $text ) {
$text = get_the_content('');
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text);
$excerpt_length = apply_filters('excerpt_length', 55);
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, '[...]');
$text = implode(' ', $words);
}
}
return $text;
}
3. If you see $excerpt_length = apply_filters(‘excerpt_length’, 55); change the text length which by default is 55 to the number of words you prefer.
—–
Mike Little suggests a better way with a lot less code and hassle involved!
Apply a filter! Alright the above code hints to you that you are applying a filter, so instead, you can leave the core files and formatting.php file alone.
Instead:
1. Go to your functions.php of your theme template and add the following
add_filter('excerpt_length', 'my_excerpt_length');
function my_excerpt_length($len) { return 75; }
Now your excerpts will be the length you want!
Related posts:








