How to Limit Excerpt Length in WordPress

To limit the excerpt length in WordPress, you can follow these steps:

  1. Open your WordPress dashboard and navigate to the Appearance section.
  2. Click on the "Editor" option to access the theme files.
  3. Locate and click on the "functions.php" file on the right-hand side of the screen.
  4. Scroll down to the bottom of the file and add the following code:
function custom_excerpt_length( $length ) {
    return 20; // Change the number to the desired excerpt length
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
  1. Adjust the number "20" in the code to the desired length of your excerpt.
  2. Save the changes by clicking on the "Update File" button.

These steps will limit the excerpt length in WordPress. The code we added in the "functions.php" file is a filter that modifies the default excerpt length. By using the add_filter() function, we are telling WordPress to use the custom_excerpt_length() function to determine the length of the excerpt.

The custom_excerpt_length() function returns the desired length of the excerpt. In this example, we have set it to 20 words, but you can change this number to your preference.

By adjusting the number in the code and saving the changes, you can customize the length of the excerpt to suit your needs.