How to Limit Excerpt Length in WordPress

The easiest way to limit excerpt length in WordPress is by adding ‘Read More’ tag in your post, however you can limit excerpt length in WordPress by controlling number of words or number of characters using Excerpt Length Filter (excerpt_length) Reference. You can also limit post excerpt length by writing your own custom excerpt. And the Read more tag at the end of the excerpt can be added with all the methods.

Updated on

The easiest way to limit excerpt length in WordPress is by adding ‘Read More’ tag in your post, however you can limit excerpt length in WordPress by controlling number of words or number of characters using Excerpt Length Filter (excerpt_length) Reference. You can also limit post excerpt length by writing your own custom excerpt. And the Read more tag at the end of the excerpt can be added with all the methods.

It is always beneficial to display excerpt on the index page of your blog or website to highlight key points of your article or blog post. Sharing the brief description of your article via excerpt engages your visitor and encourage them to read the full post.

How to Limit Excerpt Length in WordPress - Technumero
How to Limit Excerpt Length in WordPress

How to add ‘Read More’ tag in your post

You can add a ‘Read More’ tag while editing your post. Add a new post or open any previous post, write your content and

  1. First choose the word or paragraph after that you want to add ‘Read More’, take the cursor after the word where you want to add Read More.
  2. Use the Insert Read more tag button to add the ‘Read More’ link.
  3. ‘Read More’ link will be added in your post.
How to add Read More tag in your post
How to add Read More tag in your post

How to Enable Custom Excerpt option in WordPress 

Enabling custom excerpt option in WordPress gives you the liberty to write your own description or summary of your post. And it is not to mention that you can limit your writing. You need to follow the below given steps to enable custom excerpt in WordPress.

  1. Go to your WordPress dashboard,
  2. Add a new post or you can open your already created post.
  3. Click on the ‘Screen Options’ link placed at top right corner of your dashboard
  4. Check the ‘Excerpt’ box in Screen Options panel
  5. And that is it, now scroll down to find custom excerpt column.
How to Enable Custom Excerpt option in WordPress
How to Enable Custom Excerpt option in WordPress

How to Limit Excerpt Length in WordPress

There are many methods to Limit Excerpt Length in WordPress, You can limit the character length of your excerpt or limit the word length as per your desire. You can achieve it by adding a piece of code (excerpt length filter) in your functions.php file. And if you are planning to do so, I always recommend to use a child theme to edit core theme files i.e. functions.php

Method – 1

Excerpt Length Filter (excerpt_length) Reference to Limit Excerpt Length in WordPress

By default excerpt length is set to 55 words in WordPress. Use this excerpt length filter to change excerpt length to 35 words. Add the code in functions.php file of your theme.

// Filter except length to 35 words.
// tn custom excerpt length
function tn_custom_excerpt_length( $length ) {
return 35;
}
add_filter( 'excerpt_length', 'tn_custom_excerpt_length', 999 );

You can set the word length by changing return value.

Excerpt Length Filter Reference to Limit Excerpt Length in WordPress
Excerpt Length Filter Reference to Limit Excerpt Length in WordPress

Add the following code in loop.php or where you want to display the limited excerpt.

<?php echo get_excerpt(); ?>

Method – 2

Limit Excerpt Length in WordPress by number of Characters

Use this method if you want to limit post excerpt length by number of characters. Add the following code in function.php file

// Limit except length to 125 characters.
// tn limited excerpt length by number of characters
function get_excerpt( $count ) {
$permalink = get_permalink($post->ID);
$excerpt = get_the_content();
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, $count);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = '<p>'.$excerpt.'... <a href="'.$permalink.'">Read More</a></p>';
return $excerpt;
}

This code will also add a Read More link at the end of the excerpt, you can rename it as per your choice.

Limit Post Excerpt Length in WordPress by number of Characters
Limit Post Excerpt Length in WordPress by number of Characters

Call this function by adding below code in your loop.php or index.php just after the post title <?php the_title(); ?>. Change the number if you want more than 125 characters.

<?php echo get_excerpt(125); ?>

Method – 3

Limit Post Excerpt Length in WordPress by number of Words

Use this code if you want to Limit Post Excerpt Length by number of Words.

// tn Limit Excerpt Length by number of Words
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(']]>', ']]&gt;', $content);
return $content;
}

Call this function by adding the below code in loop. You can change the word limit by changing the value.

<?php echo excerpt(30); ?>

excerpt_more filter to add the ‘Read More’ after the excerpt

You might have noticed that by using the Method – 2, a ‘Read More’ tag will also get added after the excerpt. But if you use Method – 1 & 3 you have to call it separately by add a separate function in your functions.php file. In order to get a read more link please put given below code in functions.php file after post excerpt limit code.

// Filter the "read more" excerpt link
// tn excerpt more
function tn_excerpt_more( $more ) {
return sprintf( '<a class="read-more" href="%1$s">%2$s</a>',
get_permalink( get_the_ID() ),
__( 'Read More', 'textdomain' )
);
}
add_filter( 'excerpt_more', 'tn_excerpt_more' );

Hope you find this post useful, try to implement these codes in your theme and if you face any difficulty, do let us know via comment section below.

Read More:
How to Display Code Snippets in WordPress Post or Page
Most SEO friendly Permalink Structure for WordPress
How to Customize Facebook Like Box

Photo of author
Saurabh K
Saurabh K is a technology enthusiast and part-time blogger. He loves to explore the efficient use of technology and gadgets. He is an outlier and lensman. Add him in your social circle to know more.
Disclaimer: Affiliate links of some product(s) are being used on this page, if you follow the link and make a purchase, we may receive compensation from respective companies. This compensation comes at no additional cost to you.

5 thoughts on “How to Limit Excerpt Length in WordPress”

  1. Avatar of kainer

    Method two is strange.. isn’t it missing the number “125” somehwere in the code, when I want to limit the excerpt to e.g. 90 characters, how to make that happen?

    Reply
  2. Avatar of Gabrielle

    Hi, i forked your code for method 2 – using excerpt limited by number of characters. Here is new code:
    // Limit Except length to X Characters
    function gwgwpl_get_excerpt( $count ) {
    global $post;

    // If Excerpt For Post/Page NOT EXIST
    // Grab first X Characters from Content
    if ( empty( $post->post_excerpt ) ) {
    $excerpt = get_the_content();
    }

    // If Excerpt For Post/Page EXIST
    // Grab first X Characters from Excerpt
    else if (! empty( $post->post_excerpt ) ) {
    $excerpt = get_the_excerpt();
    }

    $excerpt = strip_tags($excerpt);
    $excerpt = substr($excerpt, 0, $count);
    $excerpt = substr($excerpt, 0, strripos($excerpt, ” “));

    return $excerpt;
    }

    IN THEME:

    I was thinking about +/- of using words vs characters in excerpts,
    and in the end characters always win, what is your experience in using excerpts limited by words/characters, what do you prefer and why?
    Sorry for my English, kind regard, Gabrielle

    Reply
  3. Avatar of Shubham Singh

    which plugin do you used to create table of contents

    Reply
    • Avatar of Saurabh K

      Table of Contents Plus

      Reply
  4. Avatar of Saurabh K

    Hey Mat,
    It should also work. Feel free to modify and use it.

    Reply

Leave a Comment

Your email address and website details will not be published. Required fields are marked with *.