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 …]' . '</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 …]' . '</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!














{ 19 comments… read them below or jump to the comment form to add your thoughts }
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?
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
Had to edit my comment due to me forgetting to encode the script! Doh!
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.
oops, that's the paragraph tag that WP puts around the excerpt. I guess comments strips out the HTML brackets.
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!
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.
For more information about using the_content_rss to produce your excerpts, see the Codex page: http://codex.wordpress.org/Tem.....ontent_rss
Very useful!!! Just note, in my WP script. The [...] is HTML characters […] so I did it by replacing these chars.
Good job Lynne Pope!!!!
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.
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.
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.
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.
Yes it need to be updated. The excerpt doesn't work in WordPress 2.8
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
it works..!
Thank you for the awesome code and tuts...
Thank you for this great fix for replacing the ugly & useless default [...]
Thanks for this. It's quite useful.
Just what I needed!
{ 1 trackback }
Leave a Comment