How to Control Excerpts in WordPress

An excerpt is a post summary which is being used to describe your article in a few short sentences. Those descriptions are a great way of letting your visitors see what’s the post about and decide if they’re interested in reading the rest of it before clicking the button. Also, it is an excellent tool for RSS readers.

In WordPress, excerpts can be manual or automatic, and they’re slightly different from teasers (text before <–more–> tag) just because WordPress is handling them differently. If you don’t write your own excerpt, WordPress will make a summary automatically and take first 55 words from your post.

In this article, we are about to show you how to control excerpts in WordPress, you can learn:

  • How to change excerpt length in WordPress
  • Change default excerpt length for different categories
  • Add a “Read More” link to the end of an excerpt
  • Show excerpts in WordPress pages

How to change excerpt length in WordPress

If you are new to WordPress and now you’re trying to find your own excerpt box where you want to write a manual summary, you have probably found none. That’s because excerpts are hidden by default. To make the textbox visible, while you are editing a post or writing a new one, click on “Screen Options” which can be found on top of the page where you have to check “Excerpts”. Now you’re ready to scroll under your post and write your excerpt.

But as you can see, there is no option which would simply change the length of an excerpt. That’s probably one of the reasons why you’re reading this article, and in the next few lines, we’re about to show you a quick and simple way of doing that.

  1. Go to Appearance-> Editor
  2. On the right side, find function.php file or open the file from your FTP client
  3. Copy and paste the following function:
function my_excerpt_length($length) {
  return 90;
}
add_filter('excerpt_length', 'my_excerpt_length');
  1. Save changes after which your excerpts will have a limit of 90 instead 55 words

Of course, you are free to change the number to any integer you want. But remember we’re talking about summaries – you don’t want your summary to be too short, but there’s no need for exaggerating. You can always put a “read more” link after each excerpt.

Change default excerpt length for different categories

After some time spent on your blog, some categories might require more words in excerpts and some will need shorter ones. So, let us show you how to change the length of category excerpt.

The first example will let you choose one category for which you want to set different excerpt length. Select that category, define the number of words for its excerpt and the number of words for all other categories’ excerpt:

  1. Open functions.php
  2. Copy and paste this function:
function excerpt_length_category( $length ) {
  if ( in_category( 'Reviews' ) ) {
    return 20;
  } else {
    return 60;
  }
}
add_filter( 'excerpt_length', 'excerpt_length_category' );
  1. Change category name on the 2nd line
  2. Change the length of excerpts (number of words) for that category on the line #3
  3. Change length of all other categories on the line #5
  4. Save changes

While this will be more than enough in order to change excerpt length for that one category that bothers you, it won’t help you much if you need to define the length for several categories at once. In that case, you will be needing the following:

function excerpt_length_category( $length ) {
  if ( in_category( 'Review' ) ) {
    return 35;
  } elseif ( in_category( array( 'News', 'Videos', 'Editorial' ) ) ) {
    return 60;
  } else {
    return 55;
  }
}
add_filter( 'excerpt_length', 'excerpt_length_category' );

This function will allow you to set different excerpt lengths for different categories and still let you choose the default one.

Add a “Read More” link to the end of an excerpt

Instead of displaying the entire post on your homepage, excerpts allow you to show only a part of it which can make the user interested in the article.

After users see the title, image, and an excerpt of your post, you need to inform them that they can read more about the topic by following the link to your article. If you ask us, a featured image should always lead to the main article, but you should also allow your readers to follow the “Read More” link or a button.

If your theme doesn’t have the feature already included, you should create one for yourself. In this part of the tutorial, we’re about to show you how to quickly add “Read More” link at the end of each excerpt:

  1. Open functions.php
  2. Copy and paste the code:
function excerpt_readmore($more) {
  return '... <a href="'. get_permalink($post->ID) . '" class="readmore">' . 'Read More' . '</a>';
}
add_filter('excerpt_more', 'excerpt_readmore');
  1. Change the text if you want to
  2. Add a different class if you want to style the link differently
  3. Save changes

That’s actually all there is. After you’ve have saved the changes, each and every excerpt on your WordPress powered website will now get a “Read More” text (or whatever did you write in the code above) with a link to the original post attached to it automatically.

You can check out the result by opening your homepage, blogroll or wherever you’re displaying post’s excerpts.

Show excerpts in WordPress pages

By default, WordPress doesn’t include excerpts in pages. That’s quite reasonable since pages are made to be different than posts. But in some cases, you’ll need excerpts in your pages as well.

Since there is no easy way of allowing this, i.e., there is no checkbox which you can simply click to enable excerpts for pages, we will show you the second easiest way of doing that.

No, you won’t be needing a plugin, nor you’ll have to go into detailed setups. In the following lines, we will show you a really short function which will do the job for you.

Show Excerpts in pages:

  1. Open functions.php
  2. Copy and paste the code snippet:
function wploop_pages_excerpt() {
  add_post_type_support( 'page', 'excerpt' );
}
add_action( 'init', ' wploop_pages_excerpt' );
  1. Save changes

It’s definitely not as simple as clicking the checkbox, but it wasn’t much harder than that, wasn’t it? Now that you’re done with copying the code, you can navigate to any page to test the feature.

Most probably you won’t have the excerpt shown below the page content right away. But don’t worry – you only need to allow excerpt to be displayed on the page:

  1. Scroll on top of the page
  2. Find “Screen options” tab and open it
  3. Find the “Excerpt” checkbox and mark it

Well, it seems like there was checkbox included in the process all the way!

You can now scroll down and write an excerpt of your page. Code snippets like this one can really help a lot, right?

In Conclusion

Although summaries might sound unimportant for beginner bloggers, it is important to have your excerpts in order. Whether you’re going to write summaries for each post or let WordPress handle things, it’s up to you but make sure to handle excerpts on time.

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!