Modifying WordPress excerpts

When displaying links to your posts, it’s helpful to display a short excerpt so your readers can quickly get an overall idea of the article. The built in function to display the excerpt defaults to a certain number of words and displays a “Continue Reading” link at the end. Both defaults can be modified to fit with your design by editing a couple functions in your functions.php file.

Editing Post Excerpt Length

WordPress’ built in function to display the excerpt on a page is “the_excerpt()”. The excerpt length defaults to 55 words. To change the length to 30 words add the following code to your functions.php file:

function new_excerpt_length($length) {
    return 30;
}
add_filter('excerpt_length', 'new_excerpt_length', 999 );

The last parameter to add_filter, 999 in this instance, sets the order in which this function is executed. According to www.wordpress.org “Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action. ” I had to set this to a high number or my change was overridden.

Editing “…Continue Reading” at the end of post excerpts

To change the “…Continue Reading” link at the end of post excerpts, add the following code to your functions.php file, replacing new_excerpt_more’s return string with your customized text.

function new_excerpt_more($more) {
    return '...<div><a href="'. get_permalink() . '">Read more &gt;</a></div>';
}
add_filter('excerpt_more', 'new_excerpt_more', 999);