How to control manual excerpt length?

All we need is an easy explanation of the problem, so here it is.

I have a website in which I need to control the displayed excerpt length.
Some of the posts might have manual excerpt so I can’t use the excerpt_length filter.

I can, of course, use some kind of substr(), but was looking for a more elegant solution (if such exists).

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Take a look on my answer here: Best Collection of Code for your functions.php file

If I understood your question correctly, it does what you are looking for.

Place this in functions.php:

function excerpt($num) {
    $limit = $num+1;
    $excerpt = explode(' ', get_the_excerpt(), $limit);
    $excerpt = implode(" ",$excerpt)."... (<a href='" .get_permalink($post->ID) ." '>Read more</a>)";
    echo $excerpt;

Then, in your theme, use the code <?php excerpt('22'); ?> to limit the excerpt to 22 characters.


Method 2

With the recent version of WordPress (v.3.3.0+), you can actually use wp_trim_words.

function excerpt($limit) {
    return wp_trim_words(get_the_excerpt(), $limit);

See also:

Method 3

I’d say just look at how core does it:

I took the liberty of putting the code here for ease of copying and pasting.

global $post;
if( empty($post->post_excerpt) ){
  $text = apply_filters( 'the_excerpt', get_the_excerpt() );
} else {
  $text = $post->post_excerpt;
  $text = strip_shortcodes( $text );
  $text = apply_filters('the_content', $text);
  $text = str_replace(']]>', ']]&gt;', $text);
  $text = strip_tags($text);
  $excerpt_length = apply_filters('excerpt_length', 55);
  $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
  $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
  if ( count($words) > $excerpt_length ) {
    $text = implode(' ', $words);
    $text = $text . $excerpt_more;
  } else {
    $text = implode(' ', $words);

Method 4

Simply it can be done as below .

function custom_excerpt_length( $length ) {
    return 20;
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

Reference: Codex

Method 5

Try this: You can control the amount of words the excert outputs with the filter “excerpt_length” below are a couple examples of how you can control the size based on different conditions

add_filter( 'excerpt_length', 'new_excerpt_length' );
function new_excerpt_length( $more ) {
            return 15;
        } else {
            return 45;
    } else {
        return 100;

EDIT: Crap, I just noticed that you said the filter approach was a no go. oh well this is for other people who get here via google and want this then.

Method 6

Enter this code in your functions.php

/* easy excerpt limitation
easy_excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
$excerpt = implode(" ",$excerpt);
} else {
$excerpt = implode(" ",$excerpt);
$excerpt = preg_replace('`[[^]]*]`','',$excerpt);
return $excerpt;

and use echo easy excerpt(mylimit) instead of the_excerpt()
It works fine.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply