Quick Tips: excerpt filters that give you some more control
Filter in WordPress are a joy and certainly what make the framework so flexible. Like the last post on enabling menu support, this is just a quick tips on two filters introduced in WordPress 2.8 and 2.9.
excerpt_length
The name says it all, it allowd you to change the length of the excerpt displayed. The usage is very simple:
function mytheme_excerpt_length( $length ) {
return 40;
}
add_filter( 'excerpt_length', 'mytheme_excerpt_length' );The above function changes the number of words returned by the excerpt to 40. The code goes in your theme functions.php.
excerpt_more
Again pretty self explanatory, this one changes the end of the excerpt to whatever you want. For example, to add a link you would add this to your functions.php:
function mytheme_excerpt_more( $more ) {
return ' … <a href="'. get_permalink() . '">' . __('Continue reading', 'mytheme') . '</a>';
}
add_filter( 'excerpt_more', 'mytheme_excerpt_more' );This code will add a ‘Continue Reading’ link at the end of each excerpt and change the default ‘[...]‘ at the end to ‘…’