Snippets Library

Change Excerpt Length

Posted by Editorial Staff

Controlling the excerpt length in WordPress can be done by adding a custom function to your theme’s functions.php file. Here’s a step-by-step guide on how to create your own excerpt length:

  1. Access your WordPress site’s files using an FTP client or your hosting provider’s File Manager.
  2. Navigate to the folder /wp-content/themes/ and locate your active theme folder.
  3. Inside your theme folder, find the functions.php file. If it doesn’t exist, you can create a new one.
  4. Open the functions.php file for editing. Add the following code snippet at the end of the file:
function wpr_excerpt_length($length) {
return 20; // Change this to the desired length in words.
add_filter('excerpt_length', 'wpr_excerpt_length', 999);

In the code snippet above, replace 20 with the desired excerpt length in words.

  1. Save the changes and close the file.

After completing these steps, your WordPress site will use the custom excerpt length specified in the code snippet. Keep in mind that this change will apply to all excerpts on your website. If you want to use different excerpt lengths for specific pages or post types, you may need to create additional custom functions and apply the appropriate conditional tags.

To display the excerpt in your theme’s template files, use the the_excerpt() function. If your theme doesn’t already display excerpts, you may need to edit the appropriate template files (e.g., index.php, archive.php, search.php) and add the the_excerpt() the function where you want the excerpt to appear.

Leave a Reply

Your email address will not be published. Required fields are marked *