Skip to content

WordPress Excerpt Revisited

Have you ever wanted to use the_excerpt function in WordPress but been put off by the default output? WordPress outputs [...] at the end of each excerpt, which is not very helpful especially as it is plain text and does not link to the post. For accessibility and validation purposes, the [...] is also bad news as it has no semantic meaning.

Many people use plugins, such as The Excerpt Reloaded, to manage their excerpts. However, if you are happy with the core excerpt and simply want to replace the default output with a meaningful link you do not need to use a plugin.

Open your theme's function file, which is called functions.php, and add the following code:


//function to replace invalid ellipsis with text linking to the post
function elpie_excerpt($text)
{
   return str_replace('[...]', '<a href="'. get_permalink($post->ID) . '">' . '[Read More &hellip;]' . '</a>', $text);
}
add_filter('the_excerpt', 'elpie_excerpt');


If you want the Read More link to display beneath your excerpt simply add a <br /> tag to the code, like so…


//function to replace invalid ellipsis with text linking to the post
function elpie_excerpt($text)
{
   return str_replace('[...]', '<br /><a href="'. get_permalink($post->ID) . '">' . '[Read More &hellip;]' . '</a>', $text);
}
add_filter('the_excerpt', 'elpie_excerpt');


This has been tested in WordPress 2.6 - 2.7.1

If you enjoyed this post, make sure you subscribe to my RSS feed!

Topic: WordPress
Tagged as: accessibility, Code, excerpt, functions, read more

Share on FriendFeed

{ 19 comments… read them below or jump to the comment form to add your thoughts }

  1. 1 Jay February 27th, 2009 at 3:38 pm

    This is perfect. Exactly what I want to do. However, my excerpts don't show the [...], so there's nothing to replace. I just want to add a Read More link to the end of my excerpt. How would I do that?

  2. 2 Lynne Pope February 27th, 2009 at 6:24 pm Lynne Pope

    If you are writing your own excerpt (which is always the best thing to do for both readers and SEO) then the above won't work. There's several different ways of adding a Read More link to manual excerpts but the easiest is this...

    Go to your template that is using the_excerpt and add the following directly under the call to the_excerpt:

    <div class="more"><a href="<?php the_permalink(); ? rel="nofollow">">[Continue Reading: <?php the_title() ?> ]</a>
    </div>

    You can replace the div with p, span or whatever suits your site. Change the words "Continue Reading" to whatever you like.
    Then, in your styles.css, style the .more and .more a

  3. 3 Lynne Pope February 27th, 2009 at 6:29 pm Lynne Pope

    Had to edit my comment due to me forgetting to encode the script! Doh!

  4. 4 Jay February 28th, 2009 at 3:34 am

    Thank you for the quick response! However, and I apologize for not even mentioning this in my previous post, but I would like the "Read More" tag to be within the the tags that WP puts around the excerpt. I found a semi-complicated workaround, but i wonder if there's a simple way of acheiving this.

  5. 5 Jay February 28th, 2009 at 4:05 am

    oops, that's the paragraph tag that WP puts around the excerpt. I guess comments strips out the HTML brackets.

  6. 6 glenn March 3rd, 2009 at 9:02 am

    This works perfectly. I was looking around for exactly this. Especially the ability the add the read more link on the same line as the excerpt text. Since my "Read More" link has specific formatting, I just added class="more-link" to the anchor tag to get the same formatting as the more link on the homepage.

    Thanks again!

  7. 7 Lynne Pope March 3rd, 2009 at 1:19 pm Lynne Pope

    Jay, to get it inside the HTML tags used by the_excerpt requires either a core hack or a plugin that replaces the core WordPress function. Both of these have their drawbacks. the_excerpt is one of the few functions in WordPress that doesn't have arguments.
    An alternative, which gives more control, is to use the_content_rss instead of the_excerpt. This may help you achieve what you want.

  8. 8 Lynne Pope March 3rd, 2009 at 1:27 pm Lynne Pope

    For more information about using the_content_rss to produce your excerpts, see the Codex page: http://codex.wordpress.org/Tem.....ontent_rss

  9. 9 Bo March 20th, 2009 at 3:03 pm

    Very useful!!! Just note, in my WP script. The [...] is HTML characters […] so I did it by replacing these chars.

    Good job Lynne Pope!!!!

  10. 10 Grae May 24th, 2009 at 2:36 pm

    Hi Lynne,

    I am having trouble adding the code you mentioned. I can add the function, but if put the add_filter in functions.php, I get a completely blank screen for a home page. Is there something else I should know? Thanks, for information, though I think this is good idea.

  11. 11 Lynne Pope May 25th, 2009 at 12:40 am Lynne Pope

    Check that the theme you are using doesn't already use a filter for the_excerpt.
    If the theme does not include a custom function for the_excerpt, check if any plugins do. This function is pretty well unbreakable if the code is inserted correctly in the theme's functions.php.

    It might need to be updated for WordPress 2.8 & I will be testing it when 2.8 is released.

  12. 12 steven May 30th, 2009 at 3:40 am

    Hi, very helpful plugin! But, I had a question, now I use the excerpt to make the text automatticly shorter, so this "Lorem ipsum dolor lalala" will change in to
    "Lorem ipsum [...]"
    So, is there a way to do this otherwise, or make the length of the excerpt different.

  13. 13 Lynne Pope May 31st, 2009 at 3:01 am Lynne Pope

    There are plugins around, such as Excerpt Reloaded, which allow you to specify the length of your excerpt. However, its unknown whether this will work with WordPress 2.8. WP 2.8 is planned for release in little more than a week so it might be wise to hold off installing any new plugins right now.
    Once 2.8 has been finalised I will be testing the code I have here and will also show you how to change the length of the excerpt. I just don't want to do this until the WordPress core code has stabilised.

  14. 14 Jerome June 12th, 2009 at 2:02 am

    Yes it need to be updated. The excerpt doesn't work in WordPress 2.8 :(

  15. 15 Lynne Pope June 12th, 2009 at 3:38 pm Lynne Pope

    I notice a lot of people are reporting errors with WordPress 2.8 display of the_excerpt. I suspect that these problems are being caused by plugins or by custom theme functions though as I haven't run into any issues on either a clean install of 2.8 or with upgrades from 2.7/2.7.1.
    The code I have given here works just fine on WordPress 2.8 - at least, it does with all the tests I have run ;)

  16. 16 kramero August 4th, 2009 at 10:04 pm

    it works..!
    Thank you for the awesome code and tuts... :)

  17. 17 Kim October 15th, 2009 at 3:05 am

    Thank you for this great fix for replacing the ugly & useless default [...] :)

  18. 18 Kellen December 4th, 2009 at 8:22 pm

    Thanks for this. It's quite useful.

  19. 19 Chris Reilly January 18th, 2010 at 11:22 am

    Just what I needed!

{ 1 trackback }

  1. that jon jackson » Blog Archive » Helpful Wordpress Crap. August 6th, 2009

Leave a Comment

You can 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>
Any comments that look like spam will be treated as spam - this includes SEO titles and use of spurious keywords.

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution.