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 valid output and a link you do not need to use a plugin.
Replacing Invalid Read More Output
Open your theme's function file, which is called functions.php, and add the following code.
//function to replace invalid ellipsis
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
function elpie_excerpt($text)
{
return str_replace('[...]', '<br /><a href="'. get_permalink($post->ID) . '">' . '[Read More…]' . '</a>', $text);
}
add_filter('the_excerpt', 'elpie_excerpt');
Enhance Excerpt with Link Title
“Read More …” is not just meaningless, it's a serious barrier to accessibility (not to mention, bad practise for SEO). To replace the default [...] and use post titles in your anchor link for the excerpt, add the following code to your theme's functions.php file.
//function to replace invalid ellipsis with meaningful link
function elpie_excerpt($text)
{
return str_replace('[...]', '…<br /><a href="'. get_permalink($post->ID) . '">' . 'Continue Reading:'. get_the_title() . '</a>', $text);
}
add_filter('the_excerpt', 'elpie_excerpt');
These have been tested in WordPress 2.6–2.9.2
Change Excerpt Length
Since WordPress 2.8 it has been possible to change the excerpt length with this simple function. Add this code to your theme's functions.php file and alter the length to reflect the number of words you want output in your excerpts.
function elpie_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'elpie_excerpt_length', 999 );
Tested in WordPress 2.8–3.1.3.
UPDATE:
In response to the questions many of you have about how to get these functions to work, I have added the following. I hope it helps.
Troubleshooting Excerpt Filters in WordPress
These filters have now been tested up to the latest WordPress release - 3.1.3. However, if you are experiencing problems with them please note the following:
- The functions must be added to your theme's functions.php file;
- The filters only work on excerpts that are automatically generated by WordPress. If you manually enter excerpt text the filters will not affect them;
- The filters only work on excerpts that use the_excerpt. Some themes use the_content with a <!--more--> tag. These functions do not change the "Read More" links generated by the <!--more--> tag.
- Some plugins contain code that overrides the excerpt, rendering the filters useless. Disable plugins and test;
- Some themes contain code that override the filters. Try the default theme to test if this is your issue;
- Some shortcodes interfere with filters —look for custom widgets or any shortcodes that contain code affecting the_excerpt or get_the_excerpt.
If you enjoyed this post, make sure you subscribe to my RSS feed!














{ 20 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!
Hi Lynne...Well I done did it now! I added this line:
//function to replace invalid ellipsis with text linking to the post
function elpie_excerpt($text)
{
return str_replace('[...]', 'ID) . '">' . '[Read More …]' . '', $text);
}
add_filter('the_excerpt', 'elpie_excerpt');
Into my functions php file folder and now it jacked up my page and wont let me get into the folder to remove it...now what??
Just wanted to remove the excerpt function on my posts..that only show on my blog pages...REALLY need your help!!
{ 3 trackbacks }
Leave a Comment