Editing The Number Of Words For The Excerpt

Posted by Nile | Posted in Tutorials | Posted on 11-06-2009 | Comments Off

Tags:

wordpress-tricks

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!

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • FriendFeed
  • Identi.ca
  • LinkedIn
  • Posterous
  • Reddit
  • StumbleUpon
  • Technorati
  • Tumblr
  • Twitter
  • Yahoo! Buzz

Related posts:

  1. How to Change The Default Avatar
  2. Edit This Entry Template Tag

About Nile Flores

Web and graphic designer fom Southern Illinois. I love to work with WordPress.

Connect with Nile at: Twitter | Facebook | LinkedIn

Nile has written 87 articles at WPAddict.

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Comments are closed.