How To Change The Default WordPress Excerpt Length

There may come a time when you find a theme that you like yet after trying it out you find that the excerpt length that is displayed just isn’t working for you. Whether it be too short or too long, it just needs to be changed! Fortunately it is quite easy to modify the excerpt length in WordPress even if you don’t know anything about PHP.

To begin, you need to connect to your web server either through FTP/Cpanel or any other method that you choose. Luckily we only have to mess around in one file and change only one line of code so there isn’t much to screw up.

Core WordPress files are being changed in this guide, it is recommended that you backup your blog!

Once you are connected to your web server you need to make your way to the directory that contains your WordPress files and folders. You should see wp-admin, wp-content and wp-includes to let you know that you are in the right place. From here, navigate into the wp-includes folder and look for the formatting.php file. This is the file that we are going to be modifying in order to change the default WordPress excerpt length.

As a quick note, the following code shown will be for WordPress 2.7. Other versions of WordPress have slightly different code yet should be generally the same.

Once you have formatting.php opened up and ready to edit, find the line that begins with

function wp_trim_excerpt($text) {

This line of code is the beginning of the function that sets the length of the excerpt. The entire function will look like

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;
}

The line of code that we need to edit to change the excerpt length is

$excerpt_length = apply_filters('excerpt_length', 55);

55 is the default number of words that is used to display an excerpt in WordPress when the_excerpt() is used. So to change the excerpt length, simply change the number 55 to either a higher or lower number depending upon your needs.

From here, save your file and make sure it gets uploaded and overwrites the previous formatting.php file. Now all that you have to do is view a page on your blog that contains an excerpt to see the changes.

It should also be noted that should you upgrade your WordPress blog to a newer version or perform a reinstall that you will have to go back in and change this line of code.

Post Update

Considering that you may have to reapply the changes again once you upgrade WordPress it can be a hassle by hard coding into the core files. Fortunately, Bavota San has written up a great post on how to not only easily limit the excerpt length but the length of the content as well. You can find out more information over at his post titled “Limiting the Number of Words in Your Excerpt or Content in WordPress“.

Feedback!

If you read this post and understood it great! Let me know in the comments below. If you are still having issues and can't get it figured out, you are more than welcome to leave a comment or send me an email using the contact form and I'll do what I can to help!

2 Responses to “How To Change The Default WordPress Excerpt Length”

  1. Ben says:

    You can also safely change the default for just your theme without editing any core files by adding the following to your themes functions.php:

    [code]
    function my_excerpt_length($text){
    return 10;
    }
    add_filter('excerpt_length', 'my_excerpt_length');
    [/code]

    This is an extra filter on the ‘excerpt_length’ hook that overides the default 55 word with your own limit (in this case 10).
    As it lives with your theme it doesn’t touch any core files, keeping your wordpress safe on future upgrades.

    • rami says:

      this is a good code but if the original excerpt is longer then 10 words then wordpress addes [...] .

      how do i remove the [...] ?

Leave a Reply