Musings of ErisDS
beta
ErisDS

These functions and hooks let you customise the output of the_excerpt() template tag. Just copy and paste into the functions.php file in your theme. This works for both posts and pages.

To change the excerpt length (how many words are output - the default is 55 words) , define a function which returns the number you want and then call it with add_filter() on the ‘excerpt_length’ filter as shown below.

1
2
3
4
5
function custom_excerpt_length($length)
{
return 20;
}
add_filter('excerpt_length', 'custom_excerpt_length');

By default excerpts which are generated by WordPress (as opposed to defined via the “Excerpt” box) have the string “[...]” You might want to turn this into a link to the post or page and/or change the dots to something like “more”. The code below does both of these things using str_replace().

1
2
3
4
5
6
function custom_excerpt_more($more)
{
global $post;
return str_replace('[...]', '<a href="'.get_permalink($post-&gt;ID).'">...read more...</a>', $more);
}
add_filter('excerpt_more', 'custom_excerpt_more');

You can change the second parameter of str_replace() to whatever you want to be displayed. I included the $post global so that I could get the permalink and make the string into a link to the post/page.

Please note these will not work if you have any plugin installed which removes the excerpt hooks. One such plugin is the Event Calendar 3 plugin. If you come across any more please let me know in the comments.

A Note on Snippets: When customising a CMS such as WordPress it is often the simplest pieces of code which are the hardest to either find or remember. These snippets are placed here for my own reference and will hopefully be useful to others. If you find them useful or have any suggestions, please let me know.

Related Posts

Share this...

  •  Add 'Snippet: Wordpress - Customise the Excerpt Length and More string' to Del.icio.us
  • Add 'Snippet: Wordpress - Customise the Excerpt Length and More string' to Twitter
  • Add 'Snippet: Wordpress - Customise the Excerpt Length and More string' to digg
  • Add 'Snippet: Wordpress - Customise the Excerpt Length and More string' to FURL
  • Add 'Snippet: Wordpress - Customise the Excerpt Length and More string' to reddit
  • Add 'Snippet: Wordpress - Customise the Excerpt Length and More string' to Technorati
  • Add 'Snippet: Wordpress - Customise the Excerpt Length and More string' to Newsvine
  • Add 'Snippet: Wordpress - Customise the Excerpt Length and More string' to Stumble Upon
  • Add 'Snippet: Wordpress - Customise the Excerpt Length and More string' to Google Bookmarks
  • Add 'Snippet: Wordpress - Customise the Excerpt Length and More string' to FaceBook

Comments

7 Comments to "Snippet: WordPress – Customise the Excerpt Length and More string"
  1. 15th Jan

    Mike says:

    Nice technique. I’ve never adjusted the excerpt using filters before, the code I do use is so ugly in comparison. I obviously missed the length and more hooks brought in 2.8|2.9 that make excerpt messing so much easier. Thanks for the pointer will have to make use of this in my sites spring clean. :)

  2. 16th Jan

    Rob Mason says:

    Been meaning to figure this out for a while, but this saves me the effort. Good work old girl. One quick question though: how do you do the same but for the RSS feed?

    R

  3. 16th Jan

    ErisDS says:

    @mike You’re welcome, not sure how I stumbled across it now.

    @rob I’m not sure about the rss feed. I would have thought these filters worked the same. There is a filter called the_excerpt_rss which may have something to do with it though!

  4. 17th Jan

    Mike says:

    @ErisDS @Rob
    I’ve got an adjustment on the ‘more’ text, not the length though, perhaps using substr_replace() after the trim?

    1
    2
    3
    4
    5
    6
    function feed_more( $text ) {
    $text = str_replace('[...]', '', $text);
    $text = trim( $text );
    return $text = 'ABC'; // where ABC is what you want, template tags work!
    }
    add_filter('the_excerpt_rss', 'feed_more');
  5. 4th Mar

    Jenny says:

    I’ve been searching everywhere for how to customize my WordPress excerpts, thanks for sharing!

  6. 4th Mar

    ErisDS says:

    Thanks, it’s always good to know that my content has been useful :)

  7. 25th Jun

    ErisDS says:

    I’ve just updated this post to add that the default length of the excerpt is 55 words :)

Add your thoughts

  • 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>