How To Modify “the_excerpt” in WordPress

These are a few pretty simple tricks that can actually be found if you go to WordPress and look in the reference for the_excerpt() function but I figured I would show you how I have customized it for my own installation.

The first two, in my opinion, work hand in hand because they actually modify the length and the “Read More” options of the_excerpt. These are typically the more two requested features that people like to change. For these just take the code below and drop it in your “functions.php” file located in your wp-content/themes/your-theme/ folder where “your-theme” is whatever theme you would like to have this work with.

Heres the code:

//Modify the length of the_excerpt
function new_excerpt_length($length) {
	return 150;
}
add_filter('excerpt_length', 'new_excerpt_length');
 
//Modify the "Read More" link of the_excerpt
function new_excerpt_more($more) {
	$new = str_replace("Continue reading ", "Read More", $more);
	$new = str_replace('<span class="meta-nav">→</span>', "", $new);
	return $new;
}
add_filter('excerpt_more', 'new_excerpt_more');

What this code is doing is created two new functions that will return a value to the_excerpt function when the filters are applied.

The first function if you haven’t guessed changes the length of the_excerpt so that it can show more than its default of 55 words.

The second is actually setup to take the default link that will send the user to the page with a “Read More” style link and allow the developer to change it. With the case of WordPress 3 they changed the end of the excerpt to say “Continue reading ” which we want to modify. As this changes you will just have to inspect the html of the page to find the current use and change the function replacing variables.

The other option that is available to change the_excerpt in a bigger way is to just create your own the_excerpt type of function within your “functions.php” folder. With the abilities of filters and other things its not typically as necessary.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">