Changing excerpt length in WordPress using PHP
WordPress automatically generates excerpts (short previews of posts) with a default length of 55 words. On ruachost.com, you can customize this length using a simple PHP function in your theme.
Why Change Excerpt Length?
Section titled “Why Change Excerpt Length?”- Control how much content appears in blog listings.
- Improve readability by shortening or lengthening previews.
- Enhance SEO and user experience with consistent formatting.
Steps to Change Excerpt Length
Section titled “Steps to Change Excerpt Length”-
Log in to WordPress with an administrator account.
-
In the dashboard, go to Appearance → Theme Editor.
-
Select your active theme and open the functions.php file.
-
Add the following code snippet:
// Change excerpt lengthfunction ruachost_custom_excerpt_length($length) {return 30; // set excerpt length to 30 words}add_filter('excerpt_length', 'ruachost_custom_excerpt_length');- Replace
30with the number of words you want for your excerpts.
- Replace
-
Save the file.
-
Refresh your site → Excerpts will now display with the new word count.
Important Notes
Section titled “Important Notes”- Always back up your theme before editing
functions.php. - Use a child theme to preserve changes during updates.
- If you want different excerpt lengths for specific post types, you can add conditional logic inside the function.