Customising the_excerpt

WordPress 2.9 introduced new filters to allow users to add a Read More link and customise the length of the excerpt. However, WordPress handles manual excerpts – ie those “hand-crafted” excerpts entered in the Excerpt box in the Posts Editor – differently to automatically generated excerpts. The result of this is that the 2.9 techniques need to be handled differently when using manual excerpts.

So, here’s my round-up of excerpt customisation tweaks…

  • “Auto” means that the Post author has not created a hand-crafted (ie manual) excerpt text in the Excerpt box in the Post Editor.
  • “Manual” means that the Post author has created a hand-crafted excerpt text in the Excerpt box in the Post Editor.

Auto excerpt – add a more link

Add the following to your theme’s functions.php file:

// Changing excerpt more - only works where excerpt is NOT hand-crafted
add_filter('excerpt_more', 'auto_excerpt_more');
function auto_excerpt_more($more) {
    return '&hellip; <a href="'.get_permalink().'" rel="nofollow">[Continue Reading]</a>';
}

Manual excerpt – add a more link

Add the following to your theme’s functions.php file:

// Changing excerpt more - only works where excerpt IS hand-crafted
add_filter('get_the_excerpt', 'manual_excerpt_more');
function manual_excerpt_more($excerpt) {
	$excerpt_more = '';
	if( has_excerpt() ) {
    	$excerpt_more = ' <a href="'.get_permalink().'" rel="nofollow">[Read more]</a>';
	}
	return $excerpt . $excerpt_more;
}

Auto excerpt – change length of excerpt text

By default, auto excerpts are created from the first 55 words of the Post content. To change the length of the excerpt, add this to your theme’s functions.php file, changing the number 100 to whatever you want.

// Changing excerpt length - only works with AUTO excerpt
add_filter('excerpt_length', 'new_excerpt_length');
function new_excerpt_length($length) {
	return 100;
}

Manual excerpt – change length of excerpt text

Logically, you shouldn’t need to do this – simply create the manual excerpt at the length of your choice. However, if you do want to only display part of the manual excerpt, you can do it using the following, rather clunky, workaround. Add this to your theme’s functions.php file:

// Changing excerpt length - only works with MANUAL excerpt
// Priority of 9 is set so that read more filter function fires AFTER this one.
add_filter('get_the_excerpt', 'manual_excerpt_length', 9);
function manual_excerpt_length($excerpt) {
	global $post;
	$length = 30; //Number of characters to display in excerpt
	$new_excerpt = substr($post->post_excerpt, 0, $length); //truncate excerpt according to $len
	if(strlen($new_excerpt) < strlen($post->post_excerpt)) {
    	return $new_excerpt;
	} else {
		return $excerpt;
	}
}

Note that in this case, the number assigned to the variable $length is the number of characters, not words. Also, this technique may cut a word in half – which looks weird – hence a better technique is to use one of the various Excerpt plugins. Additionally, you will notice that I have added a prioirity of “9″ to the filter call. This is to ensure that this function runs before the manual excerpt more text function described earlier.

Have fun!

Comments

  1. kamal says:

    looong time i search this code,
    Thanks

  2. Gil Namur says:

    Hi Ade,

    I will have to try this as the Genesis Featured post Widget does not offer a read more feature when selecting the excerpt.
    Think this will work in that framework?

    Cheers,
    Gil

    • Ade says:

      Hi Gil,

      Frankly, I haven’t tried this with the Genesis Featured Post Widget. Theoretically, it should work. :-)

  3. GM says:

    Great piece of code. I’ve benn triying with

    array_push($words, ‘ … Continue reading‘);

    but didn’t work. Now I finally managed to get the more link in my search results page.

    Btw, if you want your search page to show post thumbnails, you can try to add this code in the Loop:

    IMAGE URL GOES HERE

    <a href="” rel=”_self” title=”">
    <?php yapb_thumbnail('’, array(‘alt’ => ‘This is a thumbnail’), ”, array(‘w=150′, ‘q=90′), ‘thumbnail’); ?>

  4. Sean says:

    This works perfect, although i am using the_excerpt in my meta descirption for the page and this function puts a link into that as well… do you know how i could fix that problem? i wouldnt mind but the quotes in the meta get all screwed up and it ends up outputting some read more text onto the top of the page.

    if you could please email if you respond to this.

    Thanks!

  5. ArleyM says:

    Thanks! I needed that first part.

    Incidentally, to add localization for multilingual sites using MO files:

    add_filter(‘excerpt_more’, ‘auto_excerpt_more’);
    function auto_excerpt_more($more) {
    return ‘… ‘ .__(‘Read More’, ‘wpml_theme’). ‘‘;
    }

  6. Elle Billias says:

    Works perfectly for me :) Thanks

Trackbacks

Leave a Comment

*

XHTML: You can use these tags: <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="">