How to Limit Excerpt Length in WordPress

Are you looking for how to limit excerpt length in WordPress? Here’s a quick and easy way to do so without using a plugin:

1) Open your functions.php file. This file would be found in your theme directory. If you do not have a functions.php file, create one with that name.

2) Copy the following code into your functions.php file:


/* ********************
* Limit post excerpts. Within theme files used as
* print string_limit_words(get_the_excerpt(), 20);
******************************************************************** */
function string_limit_words($string, $word_limit) {
$words = explode(' ', $string, ($word_limit + 1));
if(count($words) > $word_limit)
array_pop($words);
return implode(' ', $words);
}

3) Save your functions.php file.

4) Now, you can use the new snippet below to replace the standard WordPress call for the excerpt:

Standard WordPress excerpt call:

<?php the_excerpt(); ?>

New Limit excerpt call:

<p><?php echo string_limit_words(get_the_excerpt(), 20); ?></p>

This new snippet calls our new function and displays it based on the number of words defined.

  • You can change the number from 20 to whatever number you want.
  • For your reference, by default the standard WordPress excerpt displays 55 words.
  • Note that I wrapped the template tag with paragraph tags (<p> and </p>) to replicate standard WordPress excerpt functionality. You are free to wrap your function in <div> or <span> tags based on your styling needs.

Questions and Answers

Question: What file should I change?

If you are attempting to change a specific page, you will need to replace the standard WordPress excerpt with the new snippet in the following file(s):

  • If you want to change it on the home page, then you will find one or more instances within the home.php file.
  • If you want to change it on the category archive page, then you will find one or more instances within the archive.php file.

Question: Can I use this on any theme? in any file?

Yes. This function will work in any theme, and can be used in any file within the WordPress loop query.

Let me know if this function comes in useful or if you have any specific questions. I’ll do my best to help.

WPsite.net

WPsite.net will feature articles on WordPress (themes, plugins, and tutorials, Internet Marketing, Blogging, SEO, Web Design, and Social Media

10 thoughts on “How to Limit Excerpt Length in WordPress

    1. Thanks Ryan. You’re right about that function and it’s just as useful if every excerpt needs to be same length. The solution provided above allows user to define different lengths depending on where it’s used. Though, glad you brought this up as it’s going to be helpful for people. Thanks for contributing.

  1. Just what I needed! Thank you sooooo much! All the other tricks were not helping me out when it came to having a different excerpt length for a sub-page (as appose to all pages).

    1. @akhlesh – unfortunately, there is no way to have excerpt greater than 55 words, nor is that very helpful to increase click-throughs or SEO. 55 is WordPress limit for excerpts.

    2. Charlie, that is simply not true. Using the WordPress API as I stated earlier, you can change the default excerpt length to whatever you want, including increasing it past 55 words. You can even included that piece of code in any page template instead of the functions.php and change the excerpt length on a page by page basis.

      @akhlesh – Use this function from the WordPress API to increase your excerpt past 55 words:


      function custom_excerpt_length( $length ) {
      return 100; // change this to be your desired excerpt word length.
      }
      add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

      Insert your desired length in place of ‘100’. This can be 100, 200, or whatever you want. I’ve had several clients who wanted the length to be more than 55 and this does just that.

      You can find the code here: http://codex.wordpress.org/Function_Reference/the_excerpt

  2. Thanks Ryan for correcting me. You are absolutely right that function does work for greater than 55 words.

    @akhlesh – I tested Ryan’s code above and it does exactly what you asked – excerpts greater than 55 or whatever number you want.

    Credit to Ryan. Cheers.

  3. Thanks for this! I was looking for a function that allowed me to vary excerpt length between two containers on the homepage of a theme I was using… the standard

    function custom_excerpt_length( $length ) {
    return 20;
    }
    add_filter( ‘excerpt_length’, ‘custom_excerpt_length’, 999 );

    wouldn’t work for me. Glad I found this… thanks for saving us all some time!!

  4. Thanks for this! I was looking for a function that allowed me to vary excerpt length between two containers on the homepage of a theme I was using… the standard

    function custom_excerpt_length( $length ) {
    return 20;
    }
    add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

    wouldn’t work for me. Glad I found this… thanks for saving us all some time!!

Leave a Reply

Your email address will not be published. Required fields are marked *