How to edit the WordPress excerpt length easily

How to edit the WordPress excerpt length easily

How to update PHP in WordPress while avoiding errors
January 22, 2020
WordPress interview questions you need the answers to
January 31, 2020
How to update PHP in WordPress while avoiding errors
January 22, 2020
WordPress interview questions you need the answers to
January 31, 2020
 

Can you change the length of a WordPress excerpt? The length of a WordPress excerpt plays an important role in both content and copywriting. It can attract the right readers to your articles. Usually, the title isn’t enough to really depict what your article is about to the user. This is why the excerpt is needed.

This small amount of text may bring more people to your website than a good title. If it is well-written, it shows the reader what to expect from the article and whether it will meet his/her needs.

In WordPress, the excerpt can be found in various parts of the structure of the theme , including:

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

The advantages of the excerpt are generally accepted. However, many users struggle with the benefits because of the length imposed by WordPress. By default, you can write only 55 words in the excerpt. However, this is not a dead-end.

This article will show you how to change excerpt length in WordPress in a number of ways. Now you can customize the length of this part of the text according to the particular needs of your articles.

Why do you need to change the excerpt length? WordPress guide

 

As a WordPress user, you know how different themes can change the structure of articles, including the excerpt display. However some of the default settings really don’t fit the style and the tone of your articles.

Now you don’t have to depend on those settings. The WordPress excerpt length can be customized.. Read on to find out how.

The main reason to customize is that the standard 55 words are often inappropriate for your needs.. Much depends on the style, subject, and which language you’re using. What works for one language and its alphabet may not work for another.

Let’s take an example with the EightyDays theme:

It gives you 3 lines for the excerpt, which equals around 20 words in English. However, if you write in Arabic or Russian alphabet, you would need only 2 lines for the same amount of words. This is due to the difference between the characters. In conclusion, the EightyDays theme with the standard 3 lines of excerpt wouldn’t make your blog look as good in Russian or Arabic. This is what you need to change.

One aspect that you should take into account is that, generally, you need a longer excerpt fora 1-column grid of content. That’s why, when choosing to change the layout from 2-column to 1-column, you need to also change the length of the excerpt.

Limit your WordPress Excerpt Length to a certain number of words.

If you want to set a word limit for your excerpt, use the 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;

}

You should pay attention to the_excerpt() and the_content() segments. Write into the brackets the number of words you want to limit your excerpt to.

Here is an example: excerpt(30) – this means your excerpt will have at most 30 words. Add it to the the_content() in the same way.

Limit your WordPress Excerpt Length by the number of characters.

Sometimes you will lose accuracy by making the number of words the main criterion.. With shorter words, the text will be smaller and vice versa. This looks inconsistent and unprofessional.

Another solution is to change the criterion to the number of characters. In WordPress, all you have to do is to use the following code in the setting file of your theme, no matter if it’s a classic or child theme.

/**

 * Limit excerpt to a number of characters

 *

 * @param string $excerpt

 * @return string

 */

functioncustom_short_excerpt($excerpt){

            returnsubstr($excerpt, 0, 200);

}

add_filter('the_excerpt', 'custom_short_excerpt');

200 is the number you have to replace. Choose any other number of characters that suit your needs.

The main disadvantage of this function is that most of the time, your last word(s) may be trimmed when the text exceeds the limit. However, there is a solution.

You can calculate your words and characters exactly or, even simpler, use the following code segment, which will cancel the trimming function of the software.

/**

 * Limit excerpt to a number of characters without cutting last word

 *

 * @param string $excerpt

 * @return string

 */

functioncustom_short_excerpt($excerpt){

            $limit = 200;

 
            if (strlen($excerpt) > $limit) {

                        returnsubstr($excerpt, 0, strpos($excerpt, ' ', $limit));

            }
 

            return $excerpt;

}

 add_filter('the_excerpt', 'custom_short_excerpt');

Change excerpt length with filters

There is also an easier way for you to automatically change the excerpt length without editing each file. Instead of including and editing the_excerpt() function, you can use filters.

WordPress has a very efficient filter, called “excerpt_length”. In order to enable it, copy the following snippet into your functions.php file. Then change the “20” to the number of words to which you want to limit your excerpt.

add_filter( 'excerpt_length', function($length) {

return 20;

} );

Beforehand, set your theme to run the the_excerpt function. If you use a premium one, you don’t need to follow that step, because those themes are already prepared for the function.

If you struggle to make the function work properly, try contacting your developer. There are some nuances of the code and the function that may need particular assistance.

Let the plugins from WordPress change excerpt length

There is even a simpler method to change the excerpt length – the WordPress plugins. We’ve gathered some of the most efficient ones. Choose the one that fits your needs and let it do the job.

Best 4 plugins for changing WordPress excerpt length

Change Excerpt Length

 

It’s really easy to use this plugin. It changes the Settings by adding a new feature - Reading page for excerpt length, which is exactly what you need. Change the number of words and the work is done.

Custom Excerpt Length

 

This one works on the same principle – simplicity and efficiency. Check the Settings after installing the plugin and you’ll see the new option “Reading page”. Just set the number of words.

Advanced Excerpt

 

This plugin is a little more sophisticated. Apart from the basic option of changing the excerpt length, it allows you to add some more advanced functions. Check it out and see how it can improve the general display of your blog.

Excerpt Length

 

Excerpt Length is not only about length, despite its name. It also provides some other advanced options. From this point of view, it’s similar to the one presented above.

The final option

I'm interrupting the article to tell you about BeTheme, the definitive multipurpose theme. If trying to satisfy multiple clients has become more stressful than rewarding, BeTheme is the solution for that.

BeTheme’s selection of hundreds of customizable, responsive pre-built websites is the highlight and a proven stress reducer.

The customizability of the theme makes it a dream come true for its users. There are 4 types of Page Builders that you can use with it: WPBakery, BeBuilder, and Elementor among them.

And now with the Live Builder, it’s even more impressive.

Check out BeTheme and see why our users love it!

The rest of the article is down below.

 

If you’re not into code and you don’t want to permanently edit the files to fit, then don’t be discouraged. We have another option for you.

This last solution for making your excerpt attractive, is to manually edit the text. You can do it yourself or hire someone who knows how to handle words better than you. It’s not difficult to find good content and copywriters nowadays and means you won’t need any other tools or software programs. You can change it anytime if something doesn’t go well with the display.

Ending thoughts on the WordPress excerpt length

Don’t neglect the excerpt. We know the struggle of trying to perfectly fit it into your space and design. That’s why we hope this article helped you. You have everything you needed to know to make your blog look credible and trustworthy through a good excerpt.

If you enjoyed reading this article on how to edit the WordPress excerpt length, you should check out this one about how to update PHP in WordPress.

We also wrote about a few related subjects like how to hide the page title in WordPress, how to add expires headers, how to set homepage in WordPress, how to remove query strings from static resources, how to backup a WordPress site, how to disable comments in WordPress and how to add excerpts to your pages in WordPress.

Albert Ślusarczyk
Albert Ślusarczyk
As the co-creator of Be Theme, I am a strong believer in designing with care and patience. I pour my energy, time & knowledge into perfecting the theme for our 260,000+ customers.