Default WordPress Excerpt Behavior

WordPress automatically truncates post excerpts to 55 words by default. This snippet allows you to customize that length to match your design needs.

Basic Excerpt Length Change

php
// Change excerpt length to 30 words
function custom_excerpt_length($length) {
    return 30;
}
add_filter('excerpt_length', 'custom_excerpt_length');

Dynamic Length Based on Post Type

php
// Different excerpt lengths for different post types
function custom_excerpt_length_by_type($length) {
    global $post;

    if ($post->post_type == 'post') {
        return 40; // Blog posts: 40 words
    } elseif ($post->post_type == 'product') {
        return 20; // Products: 20 words
    } elseif ($post->post_type == 'portfolio') {
        return 50; // Portfolio: 50 words
    }

    return 55; // Default for everything else
}
add_filter('excerpt_length', 'custom_excerpt_length_by_type', 999);

Custom Excerpt with Character Limit

php
// Limit excerpt by characters instead of words
function character_limited_excerpt($excerpt) {
    $limit = 150; // Character limit

    if (strlen($excerpt) > $limit) {
        $excerpt = substr($excerpt, 0, $limit);
        $excerpt = substr($excerpt, 0, strrpos($excerpt, ' '));
        $excerpt .= '...';
    }

    return $excerpt;
}
add_filter('get_the_excerpt', 'character_limited_excerpt');

Change "Read More" Text

php
// Customize the excerpt "Read More" link
function custom_excerpt_more($more) {
    global $post;
    return '... Continue Reading';
}
add_filter('excerpt_more', 'custom_excerpt_more');

Preserve HTML Formatting in Excerpts

php
// Keep HTML tags in excerpts
function keep_html_excerpt($excerpt) {
    $excerpt = strip_tags($excerpt, '');
    return $excerpt;
}
add_filter('the_excerpt', 'keep_html_excerpt');

Advanced: Create Custom Excerpt Function

php
// Custom excerpt function with multiple options
function advanced_custom_excerpt($limit = 55, $more_text = '...', $strip_html = true) {
    global $post;

    $excerpt = $post->post_excerpt ?
                $post->post_excerpt :
                $post->post_content;

    if ($strip_html) {
        $excerpt = strip_tags($excerpt);
    }

    $words = explode(' ', $excerpt, $limit + 1);

    if (count($words) > $limit) {
        array_pop($words);
        $excerpt = implode(' ', $words) . $more_text;
    } else {
        $excerpt = implode(' ', $words);
    }

    return $excerpt;
}

// Usage: echo advanced_custom_excerpt(50, '... Read More', true);

Responsive Excerpt Lengths

php
// Adjust excerpt length based on viewport (requires JS)
function responsive_excerpt_length($length) {
    // Use longer excerpts for desktop
    if (!wp_is_mobile()) {
        return 60;
    }
    // Shorter for mobile
    return 25;
}
add_filter('excerpt_length', 'responsive_excerpt_length');

Best Practices

  • Consider your design and content type when choosing length
  • Test excerpts on different screen sizes
  • Ensure "read more" links are accessible
  • Use consistent excerpt lengths across similar content types
  • Avoid extremely short excerpts that don't provide context