5 Easy Ways To Limit Post Excerpt Length In WordPress

A post excerpt is a description or summary that we show to readers to get their interest in reading.

A title may sometimes be not enough to grab the attention of readers, and it doesn’t convey the quality of content that well.

So at the various places of your website, like in the blog page, in the homepage, and in the widgets where your posts are placed, excerpts play their role in getting the interest of your readers. When we don’t want to show the full content we use post excerpt.

In a WordPress theme, it is mainly used in following ways: 

  • RSS Feeds
  • Search Results
  • Tag Archives
  • Category Archives
  • Monthly Archives
  • Author Archives

By default WordPress shows a limit of 55 words in an excerpt. However, you can customize this limit in many ways.

To show a post summary simply use the_excerpt() below post title.

In this post, I will show you…

Special Discount for SmallEnvelop Readers and Subscribers

Using my link, you will get 25% off on all InVideo plans
(Offer valid till 30th April)

Grab The Discount

A Number of Ways To Limit & Control Post Excerpt Length

  1. Limit post excerpt length or post content length using number of words.
  2. Limiting excerpt length to number of characters.
  3. Limit post summary by adding ‘read more’ tag.
  4. Enabling custom excerpt to write your own summary for each post.
  5. Control Excerpt Length using Filters

Let’s learn to do each one of these.

1. Limit Post Excerpt Length Using Number Of Words

Add following code in your functions.php file.

function excerpt($limit) {
  $excerpt = explode(' ', get_the_excerpt(), $limit);
  if (count($excerpt)>=$limit) {
    array_pop($excerpt);
    $excerpt = implode(" ",$excerpt).'...';
  } else {
    $excerpt = implode(" ",$excerpt);
  }	
  $excerpt = preg_replace('`[[^]]*]`','',$excerpt);
  return $excerpt;
}
 
function content($limit) {
  $content = explode(' ', get_the_content(), $limit);
  if (count($content)>=$limit) {
    array_pop($content);
    $content = implode(" ",$content).'...';
  } else {
    $content = implode(" ",$content);
  }	
  $content = preg_replace('/[.+]/','', $content);
  $content = apply_filters('the_content', $content); 
  $content = str_replace(']]>', ']]>', $content);
  return $content;
}

Now, in every place where you use the_excerpt() or the_content() in your loop, use excerpt($limit) or content($limit).

For example if you want to limit your excerpt length to 30 words use echo excerpt(30) and for content.
Source: Bavotasan

2. Limiting Excerpt Length To Number of Characters.

Sometimes you want to limit the post excerpt limit on character and not by words. You just want to limit the character length in excerpt.

Here is the solution. Add the following code to your function.php file

function get_excerpt(){
$excerpt = get_the_content();
$excerpt = preg_replace(" ([.*?])",'',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, 50);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
$excerpt = $excerpt.'... <a href="'.get_the_permalink().'">more</a>';
return $excerpt;
}

You can change the length 50 to your desired character length. And also ‘more’ to what text you want to display. Then call your function <?php echo get_excerpt(); ?> wherever you are planning on getting your posts.

3. Limit Post Summary By Adding read more Tag.

You can also set your excerpt length by adding more or read more tag using post editor tag option. Simply place the blinking text cursor where you want the excerpt to be stopped and click the ‘more’ tag.

insert read more tag to limit post excerpt length

4. Enabling Custom Excerpt To Write Your Own Summary

Sometimes you might not satisfied with automatically generated excerpt, which might be breaking your site layout.
There is a feature in WordPress to show an additional field for custom excerpt.

This feature isn’t activated by default but very simple to enable for any post. Click on Screen Option found above the title in post editor.

Enabling Custom Excerpts to limit post excerpt length

By enabling it there is a new field ‘excerpt’ can be seen below the content editor, as you can see in the image above. Now enter your custom excerpt with your desired length to fit your site layout.

5. Control Excerpt Length Using Filters

Excerpt length is set to 55 words by default. We can change this default value without overriding default the_excerpt() function, so that you don’t have to make a change in each files and templates where you have used the_excerpt().

To change this default excerpt length to 20 words using excerpt_length filter, add the following code to functions.php file in your theme:

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

This way you can limit post excerpts length.

Hope this article may help you to limit post excerpt length. Comments are welcome.

Bonus: Canva is an excellent tool to design blog images, social banner, business cards, posters, infographics, resumes, and other visual graphics. It has a very user-friendly drag and drop interface that makes your job easy, so anyone can easily create beautiful graphics.
Canva also provides pre-made templates for posters, logos, cards, resumes, flyers, powerpoint presentations, and many more. It’s a wonderful tool that designers and non-designers both can use to create beautiful and appealing images.

You may also refer:

62 thoughts on “5 Easy Ways To Limit Post Excerpt Length In WordPress”

  1. Found an excerpt counter online.
    Useful when writing the excerpt text.

    function excerpt_count_js(){
    
        if ('page' != get_post_type()) {
    
            echo 'jQuery(document).ready(function(){
    jQuery("#postexcerpt .handlediv").after("Excerpt length: / 125character(s).");
         jQuery("span#excerpt_counter").text(jQuery("#excerpt").val().length);
         jQuery("#excerpt").keyup( function() {
             if(jQuery(this).val().length > 125){
                jQuery(this).val(jQuery(this).val().substr(0, 125));
            }
         jQuery("span#excerpt_counter").text(jQuery("#excerpt").val().length);
       });
    });';
        }
    }
    add_action( 'admin_head-post.php', 'excerpt_count_js');
    add_action( 'admin_head-post-new.php', 'excerpt_count_js');
    
    Reply
    • you can limit the length of the widget using the widget text filter

      function smallenvelop_text_replace($text) {
      return substr($text, 0, 100) . ‘…’;
      }
      add_filter(‘widget_text’, ‘smallenvelop_text_replace’);

      Here 100 is the word count you can change this as per your requirement.

      Reply
      • Thanks for your reply. Unfortunately all of the text disappears when I use this function. Any idea why? Or something else I could try?

        Reply
      • Perhaps I could have worded this differently – the excerpt I’d like to change the length of is in a sidebar – can I do something like this? (this doesn’t work)
        `function custom_excerpt_length($length) {
        if (is_dynamic_sidebar(‘left’)) { //For the left sidebar
        return 20;
        } else {
        return 100; //for all others locations
        }
        }
        add_filter(‘excerpt_length’, ‘custom_excerpt_length’);`

        Reply
  2. Hello, i’m sorry to up this post but i have a question, i would like change the length of the excerpt and i use this function $article_nvt->post_excerpt();
    how i can change the length of the excerpt with this function ? (if you need i can put more code)

    Reply
    • Hello Shewolf,

      You can use above codes because they depend on post_excerpt() function. Don’t worry and use the solutions.

      Reply
  3. Nice writing , I was fascinated by the analysis – Does anyone know where I could find a blank Geneseo Return to Work Form: Medical Authorization copy to edit ?

    Reply
  4. Howdy! I’ve used the suggested code from #2 on my blog, but I’m having some issues with the Read More snippet. It seems to just link back to the home page of the blog, not the permalink (the href of the read more)

    Is ‘.$permalink.’ referencing something I don’t have built into my functions? OR what is going haywire here. Thanks so much for the snippet, it’s just what I was looking for!

    The site for reference is http://www.catchthecudas.com

    – Mark

    Reply
  5. Hi,

    I am having an issue with the function. I entered the code for reducing the excerpt size as it is into the functions.php (btw it says function.php on the top). After that when I substitued the_excerpt with excerpt(30) assuming that it will give me 30 words from my post instead of 55, it didn’t work. Any idea why? pasting below the code…

    ORIGINAL

    have_posts() ) : $queryxxx->the_post(); ?>
    <div class="listpages “>
    <a href="”>

    <a class="morelink" href="”>

    REVISED

    have_posts() ) : $queryxxx->the_post(); ?>
    <div class="listpages “>
    <a href="”>

    <a class="morelink" href="”>

    Any idea what am I doing wrong?

    Reply
    • Hello Ryan,
      You can modify the excerpt length on the category page by using filters.
      Use the below code, it will definitely solve your problem.
      function custom_excerpt_length( $length ) {
      if ( is_category() || is_archive() ) {
      return 20;
      }
      else{
      return 55;
      }
      }
      add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

      Please let me know if you have any problem.

      Thanks
      Poonam Namdev

      Reply
  6. Thanks for sharing!
    I think there might be an error in the second method, in this line:
    $excerpt = trim(preg_replace( ‘/s+/’, ‘ ‘, $excerpt));

    that will strip some “s” from the text.
    so I changed to:
    $excerpt = trim(preg_replace( ‘/\s+/’, ‘ ‘, $excerpt));

    hope this helps!

    Reply
  7. for some reason i still use trik Nu. 4, because i can manage what to show in excerpt, but after all.. the trik is amazing, and will try it on my other blog… thankyou Sister Poonam for this Triks… GBU

    Reply
  8. Thanks for this very useful post! Is there a way to change the excerpt length depending upon category?

    For instance, I have a blog where I’d like the excerpt to be 100 words, except for just one category where I don’t want any excerpt (0 words — just the title will show).

    Can’t quite figure out how to do that.

    Reply
    • Hello Debbie,
      You can modify the excerpt length for the specific category by using filters.
      Use the below code, it will definitely solve your problem.
      function custom_excerpt_length( $length ) {
      if ( is_category('category_id')) {
      return 0;
      }
      else{
      return 100;
      }
      }
      add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

      PS: Change the “category_id” with that specific category id and let me know if you have any problem.
      We have started a WordPress forum where we will provide help for any WordPress related issue, so please feel free to ask any question.
      http://forum.smallenvelop.com/
      Thanks
      Poonam Namdev

      Reply
  9. Hi Poonam,

    I don’t understand the line
    $excerpt = preg_replace(‘`[[^]]*]`’,”,$excerpt);

    What is it purpose?

    Thanks,
    James

    Reply
  10. Thank you for the tips. I was having a hard time keeping the html code from woocommerce excerpt and limiting it. Worked like a charm.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.