Customizing the Excerpt Length in WordPress with a Code Snippet

Excerpts in WordPress are a great way to preview a post or page without displaying the entire content. By default, WordPress automatically generates an excerpt with 55 words, but you can easily change this to suit your needs. In this article, we will show you how to change the excerpt length using a code snippet.

The code snippet we will use is a WordPress filter, specifically the “excerpt_length” filter. Filters in WordPress allow you to modify or manipulate data in WordPress, such as post content, post titles, and in this case, excerpt lengths.

Here is the code snippet that you can add to your WordPress site:

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

This code does two things:

  1. Adds a filter to the “excerpt_length” with the function name “custom_excerpt_length”. The third argument, “999”, is the priority, which determines when the filter is applied. The higher the priority, the earlier it is applied.
  2. Defines the function “custom_excerpt_length”, which takes one argument, “$length”. The function returns 20, which is the new length of the excerpt in words.

Once you have added this code to your WordPress site, the excerpt length for all posts and page previews will be 20 words. You can change the value of 20 to any number you want, depending on how long you want your excerpts to be.

In conclusion, customizing the excerpt length in WordPress is a simple and effective way to control the appearance of your post and page previews. Using the “excerpt_length” filter and a code snippet, you can change the excerpt length to any number you want, making it a powerful tool for customizing your WordPress site.

Read more from our blog

Customizing the Excerpt More […] Text in WordPress with a Code Snippet

Excerpts in WordPress are a great way to preview a post or page without displaying the entire content. By default,…

Creating Custom Themes in WordPress: A Beginner’s Guide

WordPress is a powerful content management system that allows users to easily create and manage their own websites. One of…

Want to be a part of our team?

Lunover focuses on building strong relationships within our team and investing in our team members and clients to promote job satisfaction, happiness and success.

Checkout our Jobs