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
Local Business Schema for WordPress
Add LocalBusiness JSON-LD structured data to WordPress for Google Maps and local search rich results
PHPwordpressintermediate
phpPreview
/**
* Output LocalBusiness JSON-LD Schema
* Add to child theme functions.php
* Customize the values below for your business
...#wordpress#local-business#schema+5
12/22/2025
View
Breadcrumb Schema for WordPress Without Plugins
Add BreadcrumbList JSON-LD structured data to WordPress for enhanced Google search navigation
PHPwordpressbeginner
phpPreview
/**
* Output BreadcrumbList JSON-LD Schema
* Automatically detects page type and builds breadcrumb trail
*/
...#wordpress#breadcrumb#schema+5
12/20/2025
View
FAQ Schema Markup for WordPress Without Plugins
Add FAQPage structured data to WordPress posts and pages for Google rich results without any plugins
PHPwordpressbeginner
phpPreview
/**
* Register FAQ Meta Box for Posts and Pages
*/
function faq_schema_add_meta_box() {
...#wordpress#faq#schema+5
12/18/2025
View