How to Control Excerpts in WordPress
An excerpt is a post summary that 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.
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.
- Go to Appearance-> Editor
- On the right side, find function.php file or open the file from your FTP client
- Copy and paste the following function:
function my_excerpt_length($length) {
return 110;
}
add_filter('excerpt_length', 'my_excerpt_length');
- Save changes after which your excerpts will have a limit of 110 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.
If you want even more control over your excerpts, you should consider Advanced Excerpt plugin which is capable of doing the same job with some extra features, plus, you won’t have to deal with the code and PHP files.
Change default excerpt length for different categories
After some time spent on your WordPress 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 the category excerpt.
The first example will let you choose one category for which you want to set a different excerpt length. Select that category, define the number of words for its excerpt and the number of words for all other categories’ excerpt:
- Open functions.php
- 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' );
- Change category name on the 2nd line
- Change the length of excerpts (number of words) for that category on line #3
- Change length of all other categories on line #5
- 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 WordPress 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 a “Read More” link at the end of each excerpt:
- Open functions.php
- 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');
- Change the text if you want to
- Add a different class if you want to style the link differently
- 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.
If you would like to have more control over excerpts or you don’t like messing around with the custom functions, you should take a look at Advanced Excerpt plugin.
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:
- Open functions.php
- Copy and paste the code snippet:
function wploop_pages_excerpt() {
add_post_type_support( 'page', 'excerpt' );
}
add_action( 'init', ' wploop_pages_excerpt' );
- 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:
- Scroll on top of the page
- Find “Screen options” tab and open it
- 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?
Control WordPress excerpts with Advanced Excerpt plugin
PRICE: Free
After you install and activate this free plugin, there are several options you can choose from to control your excerpts:
- Excerpt length – choose the number of characters or words that will be displayed in an excerpt
- Ellipsis – choose an HTML symbol that will be shown instead of the omitted part of the article. By default, &hellip is used to display three dots. If you’re not familiar with the code, you can find more of them here
- Finish – choose how you want your excerpt to be finished. If you let WordPress count the number of characters, a word might be cut off in the middle, or your sentence might end up unfinished. If you want to have a complete excerpt, you can choose to finish with a complete sentence, but be aware that it also means a little bit longer summary
- Read more link – choose if you want a “read more” link to appear at the end of an excerpt
- No custom excerpts – if you check this option, the plugin will automatically generate an excerpt even though there might be a custom one already typed in
- Strip shortcodes – sometimes you have to use a shortcode at the beginning of your article. But that doesn’t mean you want the shortcode to be seen in the excerpt. It is recommended to leave the check on and strip the shortcodes from your excerpt
- Filter – depending on your theme, choose a function that is being used for displaying excerpts. Some themes use the_excerpt() while some use the_content() function. There can be even a combination of these two in one theme so leave both checked if you’re not sure what you’re doing
- Disable on – for any reason, you might want to leave out the excerpt on some pages (for example your homepage or tag archive). Choose where you want to disable the plugin
If you want to learn more about the plugin and the code that’s powering it (which can be easily customized), visit the plugin’s wiki for additional documentation.
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. Hopefully, this tutorial will help you customize excerpts just the way you wanted. If you have any questions, feel free to write a comment and we’ll be happy to help.