PHPwordpressbeginner
Custom WordPress Excerpt Length
Control the length of post excerpts and customize the read more text
Faisal Yaqoob
November 2, 2025
#wordpress#excerpt#content#customization
Code
php
1 // Change excerpt length 2 function custom_excerpt_length($length) { 3 return 30; // Number of words 4 } 5 add_filter('excerpt_length', 'custom_excerpt_length', 999); 6
7 // Customize excerpt "read more" text 8 function custom_excerpt_more($more) { 9 return '... <a class="read-more" href="' . get_permalink() . '">Read More →</a>'; 10 } 11 add_filter('excerpt_more', 'custom_excerpt_more'); 12
13 // Custom excerpt by character count instead of words 14 function custom_excerpt_by_characters($text, $length = 200) { 15 if (strlen($text) <= $length) { 16 return $text; 17 } 18
19 $text = substr($text, 0, $length); 20 $text = substr($text, 0, strrpos($text, ' ')); 21
22 return $text . '...'; 23 } 24
25 // Usage example 26 // echo custom_excerpt_by_characters(get_the_excerpt(), 150);
Custom WordPress Excerpt Length
Control the number of words displayed in WordPress post excerpts and customize the "read more" text that appears at the end.
// Change excerpt length
function custom_excerpt_length($length) {
return 30; // Number of words
}
add_filter('excerpt_length', 'custom_excerpt_length', 999);
// Customize excerpt "read more" text
function custom_excerpt_more($more) {
return '... <a class="read-more" href="' . get_permalink() . '">Read More →</a>';
}
add_filter('excerpt_more', 'custom_excerpt_more');
// Custom excerpt by character count instead of words
function custom_excerpt_by_characters($text, $length = 200) {
if (strlen($text) <= $length) {
return $text;
}
$text = substr($text, 0, $length);
$text = substr($text, 0, strrpos($text, ' '));
return $text . '...';
}
// Usage example
// echo custom_excerpt_by_characters(get_the_excerpt(), 150);
Advanced: Different Lengths for Different Post Types
// Different excerpt lengths for different post types
function custom_excerpt_length_by_type($length) {
global $post;
if ($post->post_type == 'post') {
return 30;
} elseif ($post->post_type == 'page') {
return 50;
} elseif ($post->post_type == 'product') {
return 20;
}
return $length;
}
add_filter('excerpt_length', 'custom_excerpt_length_by_type', 999);
Create Custom Excerpt Function
// Get custom excerpt with specific length
function get_custom_excerpt($limit = 30, $post_id = null) {
if ($post_id === null) {
$post_id = get_the_ID();
}
$excerpt = get_the_excerpt($post_id);
$excerpt = strip_tags($excerpt);
$words = explode(' ', $excerpt, $limit + 1);
if (count($words) > $limit) {
array_pop($words);
$excerpt = implode(' ', $words) . '...';
} else {
$excerpt = implode(' ', $words);
}
return $excerpt;
}
// Usage in template
// echo get_custom_excerpt(25);
Features
- Flexible Length: Set custom word count for excerpts
- Custom "Read More": Personalize the continue reading text
- Post Type Specific: Different lengths for different content types
- Character Count: Option to limit by characters instead of words
- Reusable Function: Create excerpts anywhere in your theme
Related Snippets
WordPress Custom Breadcrumbs
Create SEO-friendly breadcrumb navigation without plugins
PHPwordpressintermediate
phpPreview
// Display breadcrumbs
function custom_breadcrumbs() {
// Settings
$separator = ' » ';
...#wordpress#breadcrumbs#navigation+2
11/15/2025
View
Create Custom WordPress REST API Endpoint
Add custom REST API endpoints to extend WordPress functionality
PHPwordpressintermediate
phpPreview
// Register custom REST API endpoint
function register_custom_api_endpoint() {
register_rest_route('custom/v1', '/posts', array(
'methods' => 'GET',
...#wordpress#rest-api#api+2
11/14/2025
View
WordPress Custom Menu Walker
Create custom navigation menu HTML structure with a custom walker class
PHPwordpressadvanced
phpPreview
// Custom Walker Class for Bootstrap 5
class Bootstrap_5_Walker_Nav_Menu extends Walker_Nav_Menu {
/**
...#wordpress#menu#navigation+2
11/13/2025
View