PHPwordpressbeginner

Custom WordPress Excerpt Length

Control the length of post excerpts and customize the read more text

#wordpress#excerpt#content#customization
Share this snippet:

Code

php
1// Change excerpt length
2function custom_excerpt_length($length) {
3 return 30; // Number of words
4}
5add_filter('excerpt_length', 'custom_excerpt_length', 999);
6
7// Customize excerpt "read more" text
8function custom_excerpt_more($more) {
9 return '... <a class="read-more" href="' . get_permalink() . '">Read More &rarr;</a>';
10}
11add_filter('excerpt_more', 'custom_excerpt_more');
12
13// Custom excerpt by character count instead of words
14function 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 &rarr;</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