How to use the excerpt_length filter in WordPress

Please note that any code provided in this article is for demonstration purposes only. We recommend that you thoroughly test any code on a staging environment before implementing it on your live website. We cannot be held responsible for any issues that may arise from the use of this code on a live website

You might be familiar with the excerpt_length filter if you’re a WordPress developer or a blogger who uses WordPress. This handy function allows you to trim or shorten a post’s excerpt to a specific length. But how exactly do you put it to use? In this article, we will demonstrate how to use the excerpt_length function in WordPress by walking you through the processes involved.

Let’s get started

Step 1: Locate your post’s excerpt

The snippet of our post needs to be located first before we can begin editing it. Your blog’s main and archive pages will each display an excerpt that is a condensed version of your post that was written using WordPress. You will need to edit your post and check for the “Excerpt” meta box, which is located on the right-hand side of the post editor screen, in order to find the excerpt for your post.

Step 2: Open your theme’s functions.php file

After you have found the excerpt for your post, it is important to open the functions.php file associated with your theme. This file may be found in the directory where your theme is stored, and it contains all of the functions that are utilised by your theme. If this is not a custom theme, then you should first create a child theme so that these changes don’t get removed when your theme is updated.

Step 3: Add the excerpt_length filter

After opening the functions.php file, it is time to add the excerpt_length filter to the file. To accomplish this, open up your functions.php file and just copy and paste the following code into it:

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

This code adds a filter to the excerpt_length hook, which allows us to set a custom length for our excerpt. In this example, we’ve set the excerpt’s length to 20 words. You can change this number to any value you like.

Step 4: Save the changes you’ve made.

After you have added the code, you must ensure that your modifications to the functions.php file are saved.

Step 5: View your shortened excerpt

Congratulations! You have done an excellent job of condensing the excerpt of your post by using the wp trim excerpt() function. Now, whenever you access the homepage or archive pages of your blog, you will notice that the excerpt of your article has been shortened to the length that you specified.

Making it more advanced

You can use other WordPress functions to customise how your excerpt works:

function custom_excerpt_length( $length ) {
    $post_type = get_post_type();
    if ( 'product' === $post_type ) {
        return 40;
    } else {
        return 20;
    }
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

In this example, the excerpt length is longer for the “product” custom post type than any other posts.

Wrapping it up

In conclusion, if you want to manage the amount of text that is included in the excerpt of your article, utilising the excerpt_length filter that is available in WordPress is a method that is both simple and effective. You may simply personalise the excerpt of your post to meet your requirements if you follow the procedures indicated in this guide and do so in order.