How to Set Custom Excerpt Length in WordPress

Home » Snippets » How to Set Custom Excerpt Length in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you tired of your WordPress excerpts always being cut off or too long? Having control over the length of your excerpts can greatly improve the presentation and readability of your website. In this article, we will show you how to customize the length of your excerpts in WordPress, so you can display just the right amount of content and entice your visitors to read more.

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

The code snippet above shows how to set a custom excerpt length in WordPress.

The excerpt is a short summary or teaser of a post that is displayed on archive pages, such as the blog page or category pages, before the full content of the post. By default, WordPress sets the excerpt length to 55 words. However, you can easily change this default length to a custom value using the excerpt_length filter.

In the code snippet, we define a function called wpturbo_custom_excerpt_length which takes a parameter $length. This parameter represents the default length value that WordPress sets for excerpts.

Inside the function, we simply return the custom length value we want to set for our excerpts. In this case, the value is set to 20.

The add_filter function is then used to attach our wpturbo_custom_excerpt_length function to the excerpt_length filter. The add_filter function takes three arguments: the filter name, the name of the function to be attached, and the priority of the filter. In this case, we set the priority to 999 to make sure our custom length is applied.

By adding this code snippet to your theme’s functions.php file or in a custom plugin, the excerpt length for your WordPress site will be adjusted to the custom value of 20 words. You can modify the value of $length parameter in the wpturbo_custom_excerpt_length function to set a different custom length for your excerpts.

That’s it! Now you have learned how to set a custom excerpt length in WordPress using code.

Register an account to save your snippets or go Pro to get more features.