Change WordPress Excerpt Length with Custom Code
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
// 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
// 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
// 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
// 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
// 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
// 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
// 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
Frequently Asked Questions
Q: What's the default WordPress excerpt length?
A: WordPress sets the default excerpt length to 55 words. This has been the standard since WordPress 2.9.
Q: Can I have different excerpt lengths on different pages?
A: Yes, you can create conditional logic in your excerpt_length filter to return different values based on post type, category, page template, or any other condition.
Q: Should I count by words or characters?
A: Words are more common and easier to read, but characters give you more precise control over excerpt length. Choose based on your design constraints - use characters if you have strict space limitations.
Q: How do I manually set an excerpt for a post?
A: In the WordPress post editor, look for the 'Excerpt' meta box (you may need to enable it in Screen Options). Any text you enter there will be used instead of the automatic excerpt.
Q: Will changing excerpt length affect SEO?
A: Not directly. However, well-crafted excerpts can improve click-through rates from archive pages and search results. Make sure excerpts are descriptive and contain relevant keywords.
Related Articles
Customize Excerpt Length
Control the length of WordPress post excerpts by modifying word count and ending text using built-in filters....
7 min readBeginnerChange Read More Link Text
Customize the "Read More" link text and styling in WordPress using excerpt_more and the_content_more_link filters....
6 min readBeginnerAdd Featured Images to RSS Feed
Learn how to automatically include featured images in your WordPress RSS feed for better content syndication and social ...
7 minBeginner