Blog: WordPress
Laptop with some code on a desk

Custom Excerpt Length In WordPress

Avatar for John Locke

John Locke is a SEO consultant from Sacramento, CA. He helps manufacturing businesses rank higher through his web agency, Lockedown Design & SEO.

Most WordPress themes have a means of displaying past articles with some teaser content, followed by a call to action link, inviting users to read further. There are two main options when it comes to outputting this teaser content, the functions the_content() and the_excerpt(). Before we go over how to set a custom excerpt length in WordPress, it’s a good idea to review the differences between these two functions.

Differences Between the_exceprt and the_content

Most WordPress themes use the_content() inside of The Loop to display preview content and then a Read More style link to the rest of the article. When using the_content, everything in the article that appears before the <--more--> tag is output, followed by a link to the rest of the article. If there is no more tag, then the whole article is output.

When the WordPress template in charge of outputting post archives is using the_excerpt(), the flow is a little different. This tag — the_excerpt — must be used inside The Loop.

If a post or custom post type has a manual excerpt, then that will be output, followed by an ellipsis inside of square brackets.

If the post has no manual excerpt, the first 55 words of the post entry will be used as the teaser content. This default length of 55 words is what we want to change, and there are a couple of ways to do this.

Different Ways To Get A Custom Length Exceprt

One method is to use the wp_trim_excerpt() function in a template. First, we store the post content in a variable. Then we echo out how many words we choose from the beginning of the post. Here, we are choosing to show the first 20 words.


<?php 
/**
* Custom Excerpt Length WordPress using wp_trim_excerpt()
* Use directly in template
*/

$content = get_the_content();
echo wp_trim_words( $content , '20' ); 
?>

Another way you can get a custom excerpt length is to use the custom excerpt length function supplied on the WordPress Codex. This code snippet goes directly into your functions.php file.

If you have manual excerpts enabled on posts or custom post types, and are calling those via the_excerpt() in your template, those will override this function and display what you have entered there.

In this case below, we are returning the first ten words of the post content. This will be followed by the [...], unless you have a custom ‘Read More’ link function added to your theme.


<?php
/**
* WP Custom Excerpt Length Function
* Place in functions.php
* This example returns ten words, then [...]
* Manual excerpts will override this
*/
function ld_custom_excerpt_length( $length ) {
    return 10;
}
add_filter( 'excerpt_length', 'ld_custom_excerpt_length', 999 );
?>

These are just a couple of ways that you can customize the length of your teaser excerpts in WordPress.

Avatar for John Locke

John Locke is a SEO consultant from Sacramento, CA. He helps manufacturing businesses rank higher through his web agency, Lockedown Design & SEO.

Join the Conversation

Your email address will be kept private. Required fields marked *.