Cheesetoast

Brain Food for Web Developers

Wordpress Logo

Limit the Number of Words in the WordPress Excerpt

Learn how to create and control post excerpts to be displayed on your WordPress blog page. I’ll also go into how you can change the ‘read more’ text that appears if the post exceeds the excerpt word limit.

Skip to code snippet

What is the post excerpt?

To avoid displaying the full contents of each article on a blog page WordPress allows users to create a post excerpt so they can summarize the content. Readers can scan each excerpt and decide whether or not to link to the full article.

There are a couple of standard ways to limit how much of an article is displayed on your WordPress blog page.

Firstly you can use the ‘more’ tag in the main body of your post, like so:

This is the introduction of my blog post.
<!--more-->
This is the rest of my post.

WordPress will interpret this tag as a break point and only the content preceding it will be displayed on the blog page.

The second method is to use the excerpt field under the post editor. If this isn’t visible in your admin area you’ll need to click on ‘screen options’ from the top right of your screen and tick the ‘excerpt’ checkbox.

The advantage to using the excerpt field over using the ‘more’ tag is that you can include content that won’t appear in the main article.

Force excerpts on your blog page

By default WordPress will display the entire post on the blog page. However, if you (or your clients) don’t use the methods described above there’s still a way to customize the content shown. By editing the index.php file (or creating your own template file) you can display an excerpt automatically.

Just swap the function:

the_content();

for:

the_excerpt();

If the user doesn’t utilize the ‘more’ tag and leaves the excerpt field empty then the first 55 words of the main article will be displayed instead. The cut-off point will be marked by […].

Limit the word count of the excerpt

The default maximum number of words shown from the post is 55 words. If you want to shorten this limit add the following code your functions.php file:

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

In this case the excerpt will be limited to 30 words.

Customize the ‘read more’ text […]

When the content of a post exceeds the word limit of the excerpt the cut-off point will be marked with […], indicating to the user that there is more to come. Luckily we can change this text to something a little more user-friendly, such as “Continue reading →” or “Read more…”.

Open up your functions.php file again and add the following:

function new_excerpt_more($more) {
global $post;
return '<a class="moretag" href="'. get_permalink($post->ID) . '"> Read more...</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');

Note that the new wording also becomes a link to the full article with a class named ‘moretag’.

Read more

Read the WordPress codex for more information on the_excerpt() and how to customize the ‘read more’ link.

Author: Graham

My name is Graham and I'm a Scottish web developer living in California. I love working with clean code to make attractive and usable websites. I'm also interested in cooking, gardening, and taekwondo.

Comments are closed.