Changing WordPress post excerpt length

Last updated on February 13, 2010. Tags: ,

The post excerpt is the preview of post as seen in the list-of-posts pages like the home page, category pages and search results usually seen below the post title. Note that some themes do not readily display the post excerpts (they usually display whole post content) but you can modify these themes to show post excerpts.

There are two methods to generate WordPress post excerpt. One is by filling up the Excerpt form found under the Add New Post / Edit Post interface. The other is by leaving the Excerpt form blank and WordPress will automatically generate the post excerpt from the first 55 words of the post. Post excerpt generated this way will end with [...].

Now, suppose you want use the second method but felt like 55 words are too long or too short for your post excerpt. You want your post excerpt to be drawn from the first n words of your post content, but this n is not the default 55-word WordPress post excerpt length.

All you need to do is to insert the following codes in functions.php:

function modified_excerpt_length($length) {
return 40;
}
add_filter('excerpt_length', 'modified_excerpt_length');

The number 40 in the second line of the code is the number of words that would constitute the post excerpt. You can change it to whatever number of words you want for your post excerpt.

My usual tip and warning for inserting codes in functions.php applies. Do not insert it in the middle of any other function that is already in your functions.php. If you cannot tell where a function starts or ends, the safest place is either right after the very first <?php or right before the very last ?>. This tweak was tested on WordPress 2.7.1 and WordPress 2.9.1. It was also tested in version 2.6.1 but it didn't work in that version.

Posted by Greten on January 31, 2010 under WordPress tweaks

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati

Related Posts

You might also be interested (randomly generated):

Post Comments

Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.





* Required. Your email will never be displayed in public.