As a WordPress user, you may already know that the WordPress core and themes automate many features that dictate how your content is displayed. A typical example is how your web pages show articles’ excerpts.
But do you know you can customize the WordPress excerpt length of your website? This article will present to you the way to do so!
- What are WordPress Excerpts?
- Why Do You Need to Change the WordPress Excerpt Length?
- 4 Different Ways to Customize the Excerpt Length in WordPress
- Final Words
What are WordPress Excerpts?
For a WordPress site, an excerpt is a summary or description displayed under an article’s title to grab visitors’ interest in reading.
A title sometimes can not be enough to grab your visitors’ attention and convey the quality of your content. But in some cases, people choose to hide page titles, of course, it only happens for special pages and in rare cases.
Therefore, at many places on your WordPress site, like your blog page, excerpts play a crucial role in garnering the interest of your readers.
In WordPress, excerpts often appear on these types of web pages:
- RSS Feeds
- Search results
- Tag archives
- Category archives
- Monthly archives
- Author archives
By default, the WordPress excerpt length is limited to the first 55 words of the posts/pages. However, you can customize it in several ways.
Why Do You Need to Change the WordPress Excerpt Length?
“The main reason for customization is that the standard limits of 55 first words do not fit your website’s style, tone, or language.” said Trevor Snarr.
What works for one language and its alphabet may not work in another. In addition, a WordPress theme can change the way your site displays excerpts.
Let’s assume your site needs three lines to display all 55 English words. However, maybe only two lines are required if you write in the Arabic or Russian alphabet. This is due to the difference between the characters each language uses. As a result, your site may look different from what you intended.
4 Different Ways to Customize the Excerpt Length in WordPress
#1: Add a ‘Read More’ tag to Your Content
You can add a ‘Read more’ tag while editing your writing to set the limit. Here is how:
- Open any existing post/page or add a new one and write your new content.
- Put the cursor after the place you want to add the tag.
- Click the ‘Insert Read More tag’ button on the toolbar.
- Click the Publish/Update button.
#2: Enable the Custom Excerpt Feature in WordPress
If unsatisfied with the auto-generated excerpts, you can use the custom excerpt feature in WordPress. It is turned off by default. But enabling it is very easy.
- Open any previous post/page or add a new one and write your new content.
- Click the Screen Options button located in the top right corner.
- Tick the Excerpt checkbox.
- Click the Screen Options button again to close the panel.
Once done, you should see a new Excerpt field below the editor, as in the image above.
Enabling this feature gives you the freedom to write your own description or summary for your articles. And that is not to mention you can limit the WordPress custom excerpt length in the process.
#3: Change the WordPress Excerpt Length Programmatically
This manual approach involves tweaking the code. Therefore, you may need some knowledge of coding to accomplish your objective.
First, you need to locate the ‘functions.php’ file to edit it. Before this step, you should activate your child theme to add custom code to make sure it works properly.
- From the WordPress dashboard, navigate to the Appearance > Theme File Editor page.
- Select the theme your WordPress site is using in the top-right corner.
- Open the ‘functions.php’ file from the right sidebar.
Limit it to a Certain Number of Words
If you want to set a word limit for the excerpts, insert the following code to the ‘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;
}
After that, you need to call these two functions in the ‘loop.php’ file. You can adjust the word limit by changing the number value.
<?php echo excerpt(30); ?>
<?php echo content(30); ?>
Limit it to a Certain Number of Characters
Sometimes you will lose accuracy with word count as your primary criterion. With shorter words, the text will be more concise and vice versa. This can make your WordPress site look inconsistent and unprofessional.
Another solution is to change the criteria to the number of characters. In WordPress, you just need to insert the following code to the ‘function.php’ file.
function get_excerpt( $count ) {
$permalink = get_permalink($post->ID);
$excerpt = get_the_content();
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, $count);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = '<p>'.$excerpt.'... <a href="'.$permalink.'">Read More</a></p>';
return $excerpt;
}
This code will also add a ‘Read More’ link at the end of the WordPress excerpt. You can customize it to fit your needs.
Finally, you need to call this function by adding the code below to the ‘loop.php’ or ‘index.php’ file.
For example, use the code below if you want the excerpts to be no more than 130 characters.
<?php echo get_excerpt(130); ?>
Use a Filter to Adjust Excerpt Length in WordPress
There is a more straightforward approach to adjusting the excerpt length in WordPress.
WordPress has a very useful filter called ‘extract_length’. To utilize it, copy and paste the following code into the ‘functions.php’ file. You can set the excerpt length by changing the return value.
function tn_custom_excerpt_length( $length ) {
return 35;
}
add_filter( 'excerpt_length', 'tn_custom_excerpt_length', 999 );
Finally, you need to add the following code into the ‘loop.php’ file or anywhere you want to limit the WordPress excerpt length.
<?php echo get_excerpt(); ?>
Use the First Paragraph of Your Articles for the Excerpts
WordPress has many other useful filters that can be applied to the excerpts. One of them is ‘wp_trim_excerpt.’
With this filter, WordPress ignores character limits and word count. Instead, it uses the whole first paragraph as the excerpt, no matter how many words it has.
In addition, WordPress will stop showing excerpts with annoying ellipsis halfway through a sentence.
All you need to do is copy and paste this code to the ‘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;
#4: Use a WordPress Plugin to Adjust the Excerpt Length
One of the easiest ways to change the excerpt length is to use a WordPress plugin. A recommended option for this would be the Advanced Excerpt plugin. It is popular, free, and easy to use.
With this WordPress excerpt plugin, you can:
- Trim the excerpt by word or character count.
- Add and customize ‘Read More’ links.
- Complete the last word of the excerpt; so it does not end halfway.
To install and activate the plugin, follow the instructions below:
- Go to the Plugins > Add New page from the WordPress dashboard.
- Type ‘Advanced Excerpt’ into the search box in the top-right corner.
- The plugin should appear in the first position. Click the Install Now button. After WordPress installs the plugin successfully, click the Activate button.
To use the Advanced Excerpt plugin, follow the guide below:
- Go to the Settings > Excerpt page from the WordPress dashboard.
- Change the available settings to your liking.
- The plugin allows you to use a character or word limit for the WordPress excerpt length.
- You can also choose to end the excerpts with a complete sentence to prevent the ‘read more’ tag from being inserted in the middle of a sentence.
- There is a setting called No Custom Excerpts. If enabled, all the excerpts will be automatically generated using the existing content. Custom excerpts included in posts are not displayed.
- And there are many more settings waiting for you to explore.
- Click the Save Changes button.
Final Words
Do not overlook WordPress excerpts. We know the pain of trying to match them perfectly into your website’s design.
But that is exactly why we hope our article has assisted you. You now know everything about WordPress excerpt length to make your site look credible and trustworthy. If you still have questions, feel free to drop them down in the comments section below.