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. looong time i search this code,
    Thanks

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

    • Hi Gil,

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

  3. 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. 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. 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. Works perfectly for me 🙂 Thanks

  7. sixbcreative says:

    I know this is an old thread, but I still need to say “Thanks!” I wasted 2 hours of my life trying to figure this out until I found your post. Thank you THANK YOU thank you!

  8. Hi, thanks for the information. I’m using a plugin called ‘Page Excerpts‘ by Adelie Design and it adds ‘Continue Reading’ at the bottom of the excerpt. I can’t see it in their plugin code so it must be part of WordPress, I have tried your code and it just adds another link to the page.

    What I want is to make the link say what ever the title is for seo purposes but can’t see where I can do it from.

    Any ideas please.

    • Hi,

      Sorry, I don’t know how that plugin works. Your best bet is to contact the plugin author for help.

  9. This is the thread I was looking for the same problem..just a question ..shall i implement the given code in function.php which is part of genesis framework as it is mentioned there not to edit that file…plz advice me ..

    • You should alsways use a child theme with Genesis, and then add the code to the child theme’s functions.php.

  10. THANK YOU! my new theme was making it impossible to add this. these lines of code just saved me a few hours!

  11. I am going to implement this on my site. If it’s work fine I will return to give a thank :p

  12. boone simpson says:

    Thanks, I was looking for code to add content to the manual excerpt.

    I don’t know why “Excerpts” don’t have a heading in the settings panel with an array of options especially since WP is so heavily used as a CMS.

    • I think you make a good point there. On the other hand, there is so much WP functionality hidden under the hood, I’m not sure one starts and where when stops. However, I do agree with you, especially as the Excerpt is such a fundamental part of the content of a post, etc.

Trackbacks

  1. Tools ‹ Life As A Human — WordPress

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=""> <s> <strike> <strong>

*


one × 5 =