Skip to content

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.

  • Control how much content appears in blog listings.
  • Improve readability by shortening or lengthening previews.
  • Enhance SEO and user experience with consistent formatting.
  1. Log in to WordPress with an administrator account.

  2. In the dashboard, go to Appearance → Theme Editor.

  3. Select your active theme and open the functions.php file.

  4. Add the following code snippet:

    // Change excerpt length
    function ruachost_custom_excerpt_length($length) {
    return 30; // set excerpt length to 30 words
    }
    add_filter('excerpt_length', 'ruachost_custom_excerpt_length');
    • Replace 30 with the number of words you want for your excerpts.
  5. Save the file.

  6. Refresh your site → Excerpts will now display with the new word count.

  • 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.