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');

Posted

in

by

Comments

22 responses to “WordPress 2.9 new excerpt filters”

  1. Nathan Rice Avatar

    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 Avatar

    Nathan, i got the same idea …

  3. JohnnyPea Avatar

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

  4. Remkus Avatar

    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 Avatar

    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 Avatar

    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 Avatar

    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 Avatar

    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 Avatar

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

  10. Devan Avatar

    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. […] enable you to change excerpt’s length and the default […] text. For more information, check WPEngineer’s post about […]

  12. paul Avatar
    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;

  13. Ken Avatar
    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)

  14. Devan Avatar

    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.)

  15. Dave Redfern Avatar

    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.

  16. Marek Avatar
    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?

  17. Art Avatar

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

    Art

  18. Michael Avatar

    @Art: Yeah, don’t use the excerpt() 😉

  19. […] to WP Engineer for the above code, which could be used on any WordPress theme (not just […]

  20. Simon Avatar

    This does not seem to work in wp3.0

  21. Michael Avatar

    @Simon, why?