Manage Multiple Excerpt Lengths

WordPress-Christmas-10Behind our 10th door of our Advent Calendar, we have a tip how to use multiple excerpt lengths. WordPress 2.9 allows you via filter to adjust excerpt more string and the length of the excerpt. But if you need several excerpt lengths for different templates, then what?

Suppose we even need an excerpt of 30 words, and once with 45 words and "..." as more string.
First, we write the filter functions with the required values.

function wpe_excerptlength_teaser($length) {
    return 45;
}
function wpe_excerptlength_index($length) {
    return 30;
}
function wpe_excerptmore($more) {
    return '...';
}

Now the new excerpt function.

function wpe_excerpt($length_callback='', $more_callback='') {
    global $post;
    if(function_exists($length_callback)){
        add_filter('excerpt_length', $length_callback);
    }
    if(function_exists($more_callback)){
        add_filter('excerpt_more', $more_callback);
    }
    $output = get_the_excerpt();
    $output = apply_filters('wptexturize', $output);
    $output = apply_filters('convert_chars', $output);
    $output = '<p>'.$output.'</p>';
    echo $output;
}

This function will be given the two filter functions. It will examine whether those features are available and if so, we applied the filter. Isn't there a length function, then WordPress applies the default value of 55 words. Thereafter, the excerpt will be taken. Then additional filters are applied (see default_filters.php). I haven't used the filter convert_smilies, it doesn't work with automatic excerpts anyways and I personally do not like wpautop because it comes with unpredictable results, especially in lists or code. That's why I wrapp the outputs with a clause.

Now we need the function in the template.

<?php wpe_excerpt('wpe_excerptlength_index', 'wpe_excerptmore'); ?>
// the other one
<?php wpe_excerpt('wpe_excerptlength_teaser', 'wpe_excerptmore'); ?>

Now, it makes no sense to write for any value of 1-55, a filter function ;) , but for 2-3 different values in different views, the solution is useful.


10 Comments
  1. Very nice. Thanks guys! :)

  2. paul says:

    that all seems like alot of work for such a simple task. i started off using the_excerpt_rereloaded() but then wrote myown as its so easy to make custom length strings within wordpress,

    function my_excerpt($excerpt = '', $excerpt_length = 50, $readmore = "Continue Reading »",$tags = '<a>') {
    	global $post;
    	$string_check = explode(' ', $excerpt);
    
    	if (count($string_check, COUNT_RECURSIVE) > $excerpt_length) :
    		$new_excerpt_words = explode(' ', $excerpt, $excerpt_length+1);
    		array_pop($new_excerpt_words);
    		$excerpt_text = implode(' ', $new_excerpt_words);
    		$temp_content = strip_tags($excerpt_text, $tags);
    		$short_content = preg_replace('`\[[^\]]*\]`','',$temp_content);
    		$short_content .= ' ... <a>'guid .'" title="'.$post->post_title.'">'.$readmore.'</a>';
    		return $short_content;
    }
    echo my_excerpt(get_the_content(), 30);
    echo my_excerpt(get_the_content(), 50);
    echo my_excerpt($your_content, 30);
  3. James says:

    Long time reader... first time posting ;)

    Just wanted to say I'm enjoying and learning a ton from this series. Keep up the great work!

    I just need for wordpress 2.9 to go ahead and get pushed out the door so I can begin playing [breaking] it and the new capabilities it provides. Know any good tutorials for setting up wp/wpmu with svn?

    Thanks again!

    -james

  4. Michael says:

    Danke Oliver, ich tue mein Bestes ;)

    @James: You are welcome!
    Sorry, but i can't help you with a svn tutorial.

  5. Bloody says:

    Thanks Michael ! ;)

    To manage my 5 excerpt lengths in different templates, I'm enjoy to have found your great solution. :)

  6. Dian says:

    Didn't worked for me?

    What do I do wrong?

    First two pieces of code went into my functions.php then the third goes into the template file?

  7. Steve says:

    Thanks for the snippet, really useful!

    Dian, what theme are you using, are there any existing functions that modify the excerpt that might be conflicting?

  8. Denise says:

    great function. solves a big problem for me. is there any way to keep the line breaks that are in the post in the excerpt as it is pulled with this function? i notice that when one manually puts the excerpt in in WP 3.0, the line breaks come with.

    thanks again. this is great stuff.

  9. Sam says:

    Great article, was easy to understand the function, I customised this to simply use the default the_excerpt, changing the default length, then simply adding 1 new custom excerpt function (wp_custom_excerpt) which was 100 chars.

    function wp_longerexcerpt($length) {
    return 100;
    }

    function wp_custom_excerpt($length_callback='', $more_callback='') {
    global $post;
    if(function_exists($length_callback)){
    add_filter('excerpt_length', $length_callback);
    }
    $output = get_the_excerpt();
    $output = apply_filters('wptexturize', $output);
    $output = apply_filters('convert_chars', $output);
    $output = ''.$output.'';
    echo $output;
    }

    thanks!

  10. Sbobet says:

    Thanks michael ;)

    need to costum excerpth lenght for sidebar and related post under loop.

1 Ping
  1. Verschiedene excerpt Längen mit WordPress 2.9 - WordPress 2.9, Filter, the_excerpt, apply_fliters - dynamicinternet
Leave a Reply