Skip to Content

How to Change the Excerpt Length in WordPress — Quick Guide

How to Change the Excerpt Length in WordPress — Quick Guide

Excerpts are part of the WordPress core framework. They help automate the creation of short summaries of your content. The purpose is to help your site load faster.

Rather than loading the full content, including images, WordPress is set by default to strip HTML from excerpts and limit the words to the first 55 words of your post.

The problem you will likely come across is that it lacks the intelligence to know when a sentence ends. Because of that, if the excerpt length is not changed, sentences are truncated midway.

It does not look pleasant, or read well, and can have a negative impact on users clicking through to read your content.

For that reason, it is wise to take a few moments to optimize your theme to support better-looking excerpts.
 

How to change the excerpt length in WordPress

For speed and ease, install the “Advanced Excerpt” plugin. For those comfortable editing theme files, filters can be added to the functions.php file. Auto-generated excerpts can have a different excerpt length than your custom excerpts. Alternatively, add the “more” tag in your WP text editor.
 

Adding filters to your theme’s functions.php file to change the WP excerpt length

Your theme’s functions.php file is where to set custom excerpt lengths.

Go to the “Appearance” menu on your left sidebar and select the “Theme Editor”.

On the right side, a dropdown box will show with all the themes you have installed. Make sure you are editing the correct theme.

Under the theme selection dropdown is a list of all your theme template files. Scroll down the list and click on the “Functions.php” template.

This is the file to add your excerpt filters to. Scroll to the end of it and paste in the code below.

function my_excerpt_length($length){
return 100;
}
add_filter(‘excerpt_length’, ‘my_excerpt_length’);

 

Click update file.

Go back to your blog page, and you should see the excerpt length has changed. If not, try adding “PHP_INT_MAX );”

add_filter( 'excerpt_length', function($length) {
return 100;
}, PHP_INT_MAX );

The PHP_INT_MAX );is a constant in WordPress to use when you need to override existing functions. Add that line of code, then click “update file” and the excerpt length will change to the value you set.

The 100 is a placeholder for the number of words to include in the summary. Change it to however many words you want to show as a post excerpt.

What the above filter is doing is changing the default excerpt length from 55 words to 100 words. This change will be applied to both the auto-generated excerpts and your custom excerpts.

You can include an additional filter to have a different excerpt length for custom excerpts.
 

How to set a custom excerpt length in WordPress

To set a custom excerpt length that is different to the automated excerpt, apply another filter with this code:

function custom_excerpt_length( $length ) {
return 120;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 300 );

What this filter is doing is limiting the default excerpt length to 120 words, however, if you have content that would benefit from a longer excerpt, a higher limit is set for the custom excerpt of 300 words.

Change the value to whatever length you feel you need.

By applying the two filters, you can benefit from the automated excerpt creation for standard posts and have a longer intro for your feature posts.

This can make it stand out more on your blog page, hopefully inviting users to click through to read your best content.
 

Nothing happens! Why can’t I use excerpts?

Some themes do not use the excerpts. It depends on the loop functions the developer used for the theme.

<?php the_content(); ?> loads the content

<?php the_excerpt(); ?> loads the excerpt.

When the the_content(); ?> function is used, the theme will show the full post. Only when the the_excerpt(); ?> function is included in the template files will the excerpt be shown.

If your theme does not support excerpts, you can either add it manually or use the “more” tag in your WP text editor.

The advantage of including the_excerpt(); function is it will automate excerpts for all of your content.

Without it, you need to add the “more” tag in the HTML of your content for every post you create.

A tedious process that is easily fixed by adding <?php the_excerpt(); ?>. Once this is added, your theme will start showing excerpts with the read more link to load the rest of the content.

The function is added to numerous template files, mainly the home page (home.php), archive.php, and category.php file templates. You can find these by going to “Appearance” and clicking “Theme Editor”.
 

Use The Advanced Excerpt Plugin to change the excerpt length in WP

The Advanced Excerpt plugin for WordPress makes changing the excerpt length a cinch!.

Go to plugins, select “Add New” and search by keyword for “Advanced Excerpt”.

 

 

Click “install” and then “Activate”.

On your left sidebar, scroll down to “Settings”, then click on “Excerpt”. This will load the settings page for the plugin.

The plugin lets you use character limits or word limits for your excerpts. You can also select to end the excerpt with a full sentence to prevent the “read more” tag from being inserted in the middle of the sentence.

There is a setting to show “no custom excerpts”. If this is enabled, all of your excerpts will be auto-generated using your existing content.

Custom excerpts included in posts are not shown. Select this box if you want to auto-generate all excerpts.
 

How to set your theme to use the first paragraph for your excerpt

WordPress has multiple filters that can be applied to the excerpt hook. One of the more useful filters is “wp_trim_excerpt”.

With this filter, it ignores word counts and character limits. Instead, it uses all of the words in your first paragraph as the text to show for the excerpt. Regardless of how many words your paragraph runs.

How the function works is by grabbing all of the text within your first set of <p> tags in the HTML editor.

To show the first paragraph, add this code to your theme’s functions.php file…

add_filter( 'wp_trim_excerpt', 'my_custom_excerpt', 10, 2 );
function my_custom_excerpt($text, $raw_excerpt) {
if( ! $raw_excerpt ) {
$content = apply_filters( 'the_content', get_the_content() );
$text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
}
return $text;

What this is doing is first checking for a custom excerpt. Where there is none available, one will be created automatically using the first paragraph in your post.

Since the function is grabbing the text between the opening paragraph <p> and the closing paragraph </p> HTML tags, if there is a shortcode used in there, it cannot read them.

The <p> tags close before the shortcode starts. If that becomes a problem (or you expect it to be a nuisance) you can add the “strip shortcodes” filter.

add_filter( 'wp_trim_excerpt', 'my_custom_excerpt', 10, 2 );
function my_custom_excerpt($text, $raw_excerpt) {
if( ! $raw_excerpt ) {
$content = apply_filters( 'the_content', get_the_content() );
$text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
$content = apply_filters( 'the_content', strip_shortcodes( get_the_content() ) ); 
return $text;
}

Once the excerpt is set to use the first paragraph, you will save time writing custom excerpts.

Instead of including a custom excerpt for every post, the first paragraph automatically becomes your excerpt. And because of that, it will stop excerpts showing with the annoying horizontal ellipsis  […] midway through a sentence.