How to change the WordPress excerpt length

Martin Brinkmann
Apr 14, 2014
Development
|
4

WordPress creates a lot of pages on a website and the majority of them are created by pulling information from single articles that you create.

There is the homepage that lists the most recent articles, categories and tags that sort them into different groups, date pages that sort them by date, and several other pages some of which are optional such as comment pages.

Most of these pages have in common that they display excerpts of articles. An excerpt uses 55 words of the beginning of articles by default which you can change by adding a custom excerpt to an article, or by modifying the length of the excerpt.

This guide explains to you how to change the excerpt length on a WordPress site.

Before we start, it may be a good idea to explain the reason behind changing the length of text snippets on WordPress.

Sometimes, you may want to display longer excerpts to make the main body of the site fit the sidebar. At other times, you may have modified the number of excerpts on a page, and want to reduce the overall length of those pages by reducing the word count of excerpts.

The screenshot below shows two excerpts on the Ghacks frontpage.

It is thankfully pretty easy to modify the excerpt length, and while you can use plugins such as Easy Custom Auto Excerpt for this, it takes just a couple lines of code to modify the length manually instead.

Note that the mentioned plugin offers additional functionality that includes selecting the alignment of text or selecting on which pages excerpts are displayed on the blog.

Manual way

1. Open your WordPress theme's functions.php file. You find that file in the themes folder usually, it if does not exist, create it.

2. Scroll all the way done until you find ?>

3. Paste the following lines of code to change the excerpt length of the WordPress site. This changes the excerpt length to 65 words. Modify the value as you see fit.

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

4.  If you also want to modify the [.....] that WordPress adds after the excerpt to indicate that there is more, add the following line of code to the same functions.php file and modify the [.....] value here.

function new_excerpt_more( $more ) {
return '[.....]';
}
add_filter('excerpt_more', 'new_excerpt_more');

5. Save the file and reload your website. If you are using a caching plugin, make sure you clear the cache before you do so as you may not see the changes otherwise immediately.

Summary
How to modify the WordPress excerpt length
Article Name
How to modify the WordPress excerpt length
Description
Walks you through the steps of modifying the size of text excerpts that WordPress displays on blog pages.
Author
Advertisement

Previous Post: «
Next Post: «

Comments

  1. Ron said on April 15, 2014 at 6:02 pm
    Reply

    I see that you’re using a child theme on your site, but just as a note to others, it is generally not recommended that theme files be directly edited. If the theme ever gets upgraded (because of feature enhancements or security patches, for example), then your changes will be lost. Instead, a child theme should be created. You then create a new functions.php file in your child theme’s folder that will be loaded in addition to the parent theme’s function.php file.
    http://codex.wordpress.org/Child_Themes

    A couple of techniques that I prefer over the default 55-word excerpt:
    1. Use of the “more” tag (<!–more–>). By inserting the more tag into your post, you can control what gets displayed as the excerpt. I like this because I can insure that a whole paragraph gets displayed, and not have the excerpt break in the middle of a sentence.

    2. Use of the manual excerpt field. If you don’t see the excerpt field on your post editor, click the Screen Options tab and check off the Excerpt box. One advantage with using a manual excerpt is that you can include HTML tags, like links or tables, without the excerpt looking strange. The default 55-word excerpt strips out HTML tags.

    In theory, the manual excerpt field is supposed to take precedence over the more tag, which takes precedence over the default 55-word excerpt, but it can vary from theme to theme. Some themes don’t recognize the more tag, others don’t recognize the manual excerpt field, others won’t let you output an excerpt at all without a code change (I hate that the latest Twenty nn themes ignore the setting under Settings > Reading to display only a summary; you’d think that a WordPress-developed theme would utilize basic WordPress options). Some themes won’t strip out the HTML tags if you use the more tag. You just have to try it out on your own theme and see what works.

    1. Martin Brinkmann said on April 15, 2014 at 6:34 pm
      Reply

      Ron good points, thanks!

  2. ilev said on April 14, 2014 at 6:11 pm
    Reply

    Martin,
    Regarding WordPress, have you read this ?

    New Bing Translator Plugin lets users localize your WordPress site into the language of their choice

    Microsoft Open Technologies, Inc. has released a new Bing Translator plugin that lets you apply the power of Bing Translator to any WordPress site running version 3.8 or later. Visitors can translate your site into their preferred language in one click without leaving the page once this light-weight, cross-browser plugin is installed. This plugin also provides options for a setting a color scheme, as well as an option to allow visitors to suggest translations…

    http://msopentech.com/blog/2014/04/10/new-bing-translator-plugin-lets-users-localize-your-wordpress-site-into-the-language-of-their-choice/

    1. Martin Brinkmann said on April 14, 2014 at 7:11 pm
      Reply

      Ilev yes I got your email. I remember that translation plugins were available back in the good old days as well, but that they inflated the page count of your site. I think that it is better not to use a plugin for that no matter how useful it may be.

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

We love comments and welcome thoughtful and civilized discussion. Rudeness and personal attacks will not be tolerated. Please stay on-topic.
Please note that your comment may not appear immediately after you post it.