Quick Tip #8 – Limit the length of ‘the_excerpt’ in WordPress

Posted on

Right here's the scenario. On one of your clients post page's you use the_excerpt function to display an excerpt for each post. You've got room for a decent size excerpt and explain to them they can put about a paragraph into each excerpt, 100 words or so.

They then ask for a mini news feed that they want to appear on the homepage. You go ahead and add it and all of a sudden you've got paragraphs of text cluttering up the home page. You can either now go and tell them they can only use 10 words in each excerpt (which will look ridiculous on the posts page) or explain to them how to create a custom field which is an excerpt of the excerpt... or...

Rather than confuse them we're just going to create a really simple way to limit the amount of words when displaying the_excerpt, we're not limiting the word count from the posts edit screen remember. Just the small excerpt that we want to appear in mini news feed on the home page, only about 10 words.

Open up functions.php

Let's start by writing our function that will shorten the_excerpt. Open up functions.php from your theme folder and add this function in. The explanation is commented for you.

function limit_words($string, $word_limit) {
	// creates an array of words from $string (this will be our excerpt)
	// explode divides the excerpt up by using a space character
	$words = explode(' ', $string);
	// this next bit chops the $words array and sticks it back together
	// starting at the first word '0' and ending at the $word_limit
	// the $word_limit which is passed in the function will be the number
	// of words we want to use
	// implode glues the chopped up array back together using a space character
	return implode(' ', array_slice($words, 0, $word_limit));

Go to your loop where you're using the_excerpt

I'm doing this in the mini news feed loop. So find:

And replace it with this:

First we echo our function (as it returns a value), we then pass the string through that we want to chop. You have to use get_the_excerpt as this returns the excerpt rather than echo'ing it like the_excerpt does. Finally we pass the number of words we want, in this case 10.

Voila! Easy right? Have any questions please leave them in the comments.


  • Spyro wrote on

    Hi , unfortunately I can’t do it :( … I post the function in the functions.php and then I replace the with the one you say above and i get fatal error. what do i do wrong? check this out – http://thenoughtiespage.info/tag/madonna/

    • Nouveller wrote on

      Are you running PHP 4 or above?

  • Ronaldo Bitencourt wrote on



  • Jay Tillery wrote on

    Isn’t it easier to just use the_content_rss(‘more…’, ”, ”, 10); to limit the number of words on the homepage? That way you don’t get that [...] and you can place whatever you want for the more text.

    • Nouveller wrote on

      It would be easier yes but It’s more about learning how the script works and how you could then apply it else where.

    • Jim wrote on

      the_content_rss is depreciated, which means it is not a long term solution

  • Ttravis Stowe wrote on

    if you want to have the [...] after the words. All you have to do copy this.


    All you have to do is place the [...] after the echo function. It will show up right after the limited word count you choose. I replaced the ’10′ word count with ’150′ to give more wording to pull.


    • Ttravis Stowe wrote on


      the code despaired. : )

      echo limit_words(get_the_excerpt(), ’150′); ?> [...]

      • Nouveller wrote on

        Thanks for the tip! :)

      • Hardik Chauhan wrote on

        You can also used the substr function…

    • Fernandez wrote on

      Very smart! Thanks for the tips!! ;)

  • Thiramiro wrote on

    Works perfectly.


  • age wrote on

    thx for the tip,
    but.. how if some post doesn’t have any excrept?

    i can not simply wrote:

    the word is still cut into default WP 55 chars (with the [...]


  • age wrote on

    update ->the code is:

    if (the_excrept!==”"){
    echo limit_words(get_the_excerpt(), ’25′);
    echo limit_words(the_content, ’25′);

    • Ttravis Stowe wrote on

      nice, I didn’t think about that. thanks for the share.. : )

    • Hardik Chauhan wrote on

      Nice logic really awesome…

  • Gojeg wrote on

    Nice trick! I will try it.. :D

    • Nouveller wrote on

      No there isn’t, but you can apply the technique in other applications – not just WordPress. It’s good to know how things work.

    • Nouveller wrote on

      Plus I think what you posted will globally change the_excerpt. In this case I needed to modify one particular excerpt without changing the other.

  • Pingback: wp-popular.com » Blog Archive » Quick Tip #8 – Limit the length of the_excerpt’ in WordPress | Nouveller

  • Mike Freeman wrote on

    Awesome, thanks! What about changing it so it shows characters, not words?

    • Nouveller wrote on

      Thanks for the comment Mike, you should check out the substr PHP function, that should do away with any un-wanted characters.

  • sagive wrote on

    thanks man.. exactly what i searched for. maybe you could include in this post a link to add “read more” link in the end

  • Nick wrote on

    Tried a number of different ways to do this…yours finally worked. Thanks!

  • james wrote on

    thanks – great help!

  • dh wrote on

    Awesome, thanks.

  • Katie @ women magazine wrote on

    Excellent tips for trimming the excerpt lenght. I also found that you can change it via

    add_filter('excerpt_length', 'my_excerpt_length');
    function my_excerpt_length($length) {
    return 10; // Or whatever you want the length to be.

  • teknomodel wrote on

    thanks for code.. ;)

  • Sam wrote on


  • Sam wrote on

    this is a befokte example

  • yash wrote on

    you made my day thanks i was searching this post but how to add [...] after excerpts

  • Navaneethan wrote on

    dude superb… Its working good.. you Have any wordpress tips just mail to me… I am new for wordpress not for PHP

  • Greg wrote on

    Possibly a bit late to the party, seriously useful post man. Am brand new to WordPress but you learn what work tells you to, haha. I got this all working nice and quickly thanks to you, good job!

  • Rakesh wrote on

    Excellent tips for trimming the excerpt lenght. I also found that you can change it via this post

  • Rakesh wrote on

    Thanks help for this post
    you can add this post read more option

  • Sharon wrote on

    Thank you Dan – really quick and easy to implement.

  • Misha wrote on

    Hey Dan, I just wanted to say thank you!! You really helped me with this quick and easy fix! I am thankful the more that I don’t have any knowledge of php whatsoever! And still was able to implement your code! Your are the master! :) Thank you again! All the best from Misha

  • Misha wrote on

    Ha, sorry for getting wrong your name! I wanted to say thank you, Benjamin!

  • Magdalene Capicotto wrote on

    Oh, I remember those 3d’s from back then, on tv and in the cinema once. The glasses itself were already great fun. I don’t miss dimension on tv right now, but hey .. might be nice towatch now and then. Those cinema’s which give you movies all around you .. I think those more cool. They make me dizzy and such, certain scenes (flying above water and stuff like that).

  • Plaidmelon wrote on

    Thanks for the code, I was originally using substr, but I like this method better.

    There was some discussion regarding adding the ellipsis(…). I also wanted to add the ellipsis to the end, but got to thinking, what if my excerpt is shorter than the word limit. The ellipsis would be pointless. So I modified the function a bit, and the call to it in the template file. I figured I could let the function do the echo of the excerpt, much the way wordpress’ own functions do.

    Anyway, thought I’d share.

    First, the code for functions.php:

    function custom_excerpt($string, $word_limit) {
    $words = explode(‘ ‘, $string);

    $count = count($words);
    if ($count > $word_limit) {
    echo (implode(‘ ‘, array_slice($words, 0, $word_limit)) . ‘…’);
    else {
    echo implode(‘ ‘, array_slice($words, 0, $word_limit));

    and here’s the code for the template file:

    if (the_excerpt !== “”) {
    custom_excerpt(get_the_excerpt(), ’100′);

    else {
    custom_excerpt(the_content, ’10′);

  • Pingback: make money at homeblogging tips

  • Pancho wrote on


    Great code and it works, but I have a problem. If the content have a italic () text at the beging of the text and the closing tag of this text is further than the count limit it show in the theme only the first tag () and the rest don´t so it´s an open tag afecting all theme design.

    Any idea of how to close those tags?

    Much thanx and Happy New Year :9

  • empresa de reformas Barcelona wrote on

    You can certainly see your skills within the work you write. The world hopes for more passionate writers such as you who are not afraid to mention how they believe. At all times follow your heart.

  • Gab wrote on

    This is very nice, thanks a lot.
    Been looking around for another way to reduce the excerpt length without using the wordpress filter (due to using the_title and the_ excerpt inline, using the WP filter still keeps a line break after the_title…)
    this works perfectly ;)
    thanks again.

  • Pingback: social crm|free social crm tools|social crm tools|social media|social crm software|social crm companies|social crm market size|social crm market|social crm wiki|social crm nyc|social networking crm|social crm definition|collective intellect social media c

  • Premium Wordpress Themes | Iphone 4s Themes and Wallpapers wrote on

    That really helps me a lot. Is there any way to change the excerpts length to zero. No excerpts at all ?

  • Kyle wrote on

    Exactly what I was looking for and short, clean code. Thanks!

  • Ashkar wrote on

    Hi great.. Helped me.. :) Looking forward for more useful tips..

  • Brenian wrote on

    hey dude,

    this helped me ALOT~!!!
    been scratching my head for HOURS trying to reduce the text shown by excerpts,

    WORDPRESS should just put this function in their get_the_excerpts~!!!

    OMG spent like 3 hours trying out~!!!
    but yours was the most straight forward!!!!

    OMG Thank YOU~!!!

  • Anil wrote on

    the content can be limited by writing the code in function.php as follow

    function new_excerpt_length($length) {
    return 20;
    add_filter(‘excerpt_length’, ‘new_excerpt_length’);

  • Kyle wrote on

    Great Work! easiest and simplest answer I’ve found.

    And to add a ellipsis at the end simply add:

    return implode(‘ ‘, array_slice($words, 0, $word_limit)) . ‘…’;


    • Kyle wrote on

      Actually to make this even better use:

      if (count($words) > $word_limit ) {$end = ‘…’;}

      return implode(‘ ‘, array_slice($words, 0, $word_limit)). $end;

      This way if there is no need for the ellips then it is not added.

Add your comment

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>