When design a new WordPress theme, I believe that you will have faced with the problem that your theme needs multiple post excerpt lengths to create a beautiful appearance, for example: you want your theme to show the post excerpt for 50 characters in this section, while it needs to show up to 200 characters in other sections of your website. In this kind of situation, maybe my article will help you a lot.

Change WordPress excerpt length

By default, excerpt length is set to 55 words. To change this excerpt length you can add the following code to functions.php file in your theme:

function new_excerpt_length($length) {
return 30; // will return the 30 characters post excerpt, change this to suit your needs
}
add_filter('excerpt_length', 'new_excerpt_length');

Create multiple post excerpt lengths

In this case, you can add the following code to functions.php file in your theme (code found on http://codex.wordpress.org):

function new_excerpt($charlength) {
$excerpt = get_the_excerpt();
$charlength++;
if(strlen($excerpt)>$charlength) {
$subex = substr($excerpt,0,$charlength-5);
$exwords = explode(" ",$subex);
$excut = -(strlen($exwords[count($exwords)-1]));
if($excut<0) {
echo substr($subex,0,$excut);
} else {
echo $subex;
}
echo "[...]";
} else {
echo $excerpt;
}
}

Now you can use the function above in your theme file, for example:

  • new_excerpt(100): return 100 characters post excerpt.
  • new_excerpt(50): return 50 characters post excerpt.
  • and so on…

11 thoughts on “How to create multiple post excerpt lengths in WordPress”

  1. Thank you Jenni for this quick and very effective solution. Is this solution version-dependent or can it be used with any version of wordpress? Showing the right kind of excerpts is very important part of a wordpress theme and I hope to get some highly valuable benefits from your post.

  2. Jenni,

    This helped me have one excerpt size for the main loop content and a different excerpt size for a widget.
    Thanks for sharing

  3. Hi

    I have used

    function new_excerpt($charlength) {
    $excerpt = get_the_excerpt();
    $charlength++;
    if(strlen($excerpt)>$charlength) {
    $subex = substr($excerpt,0,$charlength-5);
    $exwords = explode(” “,$subex);
    $excut = -(strlen($exwords[count($exwords)-1]));
    if($excut<0) {
    echo substr($subex,0,$excut);
    } else {
    echo $subex;
    }
    echo "[…]";
    } else {
    echo $excerpt;
    }
    }

    and

    but i want more text but not show 317 over
    Please help me

  4. Jenni R

    This is some great information. I am a little confused with the jargon. When you say excerpt are you speaking of the comment field? Also do you have any online examples of this and what it does? I can’t wait to check out WPSubscribers

    thanks

    sean

  5. Hello Jenni,

    I would like to ask you, how to get excerpt for opengraph meta description? I use platinum seo pack, unfortunately I don’t know how to combine PSP description to Open graph. And I use your code above and it is also fail, just show up blank space. Could you help me, please?

    1. Hello Sarah, actually I’ve not thought about get the excerpt for opengraph meta description, really sorry.

  6. Adedoyin Kassem

    I have an issue with this solution herewith specified, and it is basically the logic behind displaying the “Post title” too when you call the new function.
    I mean, the function is very straight forward but, when i use it it simply pulls out everything on the post (or page in my case) from the title to the content and strips it to the specified character length… Or am I missing something here?

Leave a Comment

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