How to Customize Excerpt Length in WordPress

Learn how to customize the excerpt length in WordPress by adding a simple code snippet to your functions.php file.

Are you looking to control the length of excerpts displayed on your WordPress site? By default, WordPress displays 55 words in excerpts, but you can easily customize this to better suit your content and design. In this guide, we’ll show you how to change the excerpt length by adding a few lines of code to your theme’s functions.php file.

Why Customize Excerpt Length?

Customizing the excerpt length allows you to:

  • Improve the readability of your posts.
  • Ensure consistency in your design.
  • Provide a better user experience by controlling the amount of text shown.

Adding the Code to functions.php

To change the excerpt length, you need to add a custom function to your theme’s functions.php file. Follow these steps:

  1. Access Your Theme Files: You can do this via FTP or directly from your WordPress dashboard by navigating to Appearance > Theme Editor.
  2. Edit the functions.php File: Find the functions.php file in your theme’s directory and open it for editing.
  3. Add the Custom Code: Insert the following code snippet into the functions.php file:
function custom_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

This function sets the excerpt length to 20 words. You can adjust the number 20 to any length you prefer.

  1. Save Changes: Once you’ve added the code, save the functions.php file.

Explanation of the Code

  • custom_excerpt_length Function: This function takes the default excerpt length and changes it to the value you specify (in this case, 20 words).
  • add_filter Hook: This hook tells WordPress to apply your custom function to the excerpt_length filter.

Conclusion

By adding this simple code snippet, you can customize the excerpt length on your WordPress site to better match your content and design preferences. Remember, it’s always a good idea to back up your site before making any changes to your theme files.

If you have any questions or run into any issues, feel free to leave a comment below. Happy customizing!

We will be happy to hear your thoughts

Leave a reply

Experience the Power of Preinstalled OS Drives and Booting Guides
Logo
Compare items
  • Total (0)
Compare
0
Ninja Silhouette 9 hours ago

Joe Doe in London, England purchased a

Joe Doe in London?

Joe Doe in London, England purchased a

Joe Doe in London?

Joe Doe in London, England purchased a

Joe Doe in London?

Joe Doe in London, England purchased a

Shopping cart