WordPress 2.9 new excerpt filters

In WordPress 2.9 there will be two new filters to adjust an excerpt (the_excerpt). Previously with the_excerpt cut off at a maximum of 55 words and add a [...]. These filters are in WordPress 2.9 expandable. To change the values, you write two functions in your theme functions.php:

// Changing excerpt length
function new_excerpt_length($length) {
	return 40;
}
add_filter('excerpt_length', 'new_excerpt_length');

// Changing excerpt more
function new_excerpt_more($more) {
	return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');

Comments are closed.

22 comments

  1. Nathan Rice

    Would be a lot more exciting if they combined the filters above with the ability to define the excerpt length and appendage as function parameters like so ...

    the_excerpt(40, '...');

  2. Michael

    Nathan, i got the same idea ...

  3. JohnnyPea

    I think it would be great if I can define what HTML tags are accepted. But these are great filters anyways...

  4. Remkus

    Yeah, both enabling the HTML tags and the paramater settings would be what I think the_excerpt should be able to handle. Now I have to use plugins/ extra code on almost every WP installation...

  5. Matt Wiebe

    Nathan's right, this isn't that useful unless we can set these as function attributes as well. One size does not fit all.

  6. Alex Cragg

    I also feel that being able to use the_excerpt with a custom css class would have been a useful introduction. Having to build my own using get_the_excerpt just feels wrong...

  7. Tomas Kapler

    what i hate more is that the only option is filter by words. Quite often i need to filter by chars, and the most common need (for me) is to filter by chars with keeping full words only.

    So I am afraid i will still need to have own function.

    But I agree with others, that simple function, with option to get excerpt with defined length would be fine

  8. Devan

    This already appears in the codex at http://codex.wordpress.org/Template_Tags/the_excerpt -- but I can confirm it does not work in version 2.8.4.

  9. Michael

    Yes, it does not work in 2.8. Its a feature of WordPress 2.9

  10. Devan

    Yes, I got that. My point was to complain that the codex points to future functionality as though it already exists. Could've made that more clear.

  11. paul

    there is no point in using the_excerpt() as its SOO inflexible. there is of course the_excerpt_rereloaded() which rocks, or you can make your own, which is pretty easy to, grab the content with get_the_content(), explode() it into an array on the space, then pop() the end of the array off...and implode() it back into a string, strew the_excerpt()

    $excerpt = get_the_content();
    $new_excerpt = explode(' ', $excerpt, 50);
    array_pop($new_excerpt);
    $new_excerpt = implode(' ', $new_excerpt);
    echo $new_excerpt;

  12. Ken

    The codex is written by the community, and we'd love to have the community clean it up, update it, and make it more clear (hint hint)

  13. Devan

    Hi Ken,

    Thanks for the hint!

    In case it's useful, I'll share my perspective: What's keeping me from editing that moment in the codex is that I'm not sure what it *should* say.

    Would it be best to say that the feature isn't yet available? To remove it altogether?

    Learning the conventions of editing the codex is a bit of a steep barrier to entry for somebody who probably won't be in a position to contribute very often. (I'm a hobbyist, basically.)

  14. Dave Redfern

    I currently use something similar with 2.8:-

    add_filter('excerpt_length', 'my_excerpt_length');
    function my_excerpt_length($length) {
    return 0;
    }

    you can always add a if statement inside that for each category. but nathans idea would be nicer.

  15. Marek

    I have succesfully got this to work but my question is...

    How the hell can i make the new .... clickable to go to the specific post, as i have replaced it with ...read more

    I have managed to style it fine but being able to style it is beyond me, please help?

  16. Art

    To throw a curve ball into the mix.... is there a way to simply disable the excerpt functionality?

    Art

  17. Michael

    @Art: Yeah, don't use the excerpt() ;)

  18. Simon

    This does not seem to work in wp3.0

  19. Michael

    @Simon, why?

3 pingbacks

  1. Wordpress 2.9 Coming Soon – Features, Tips and Screenshots
  2. Artiklar för Wordpress tema- eller plugin-kodaren | jenst.se
  3. Dissecting Astatic – Thematic Child Theme Tutorial | Theme Lab