There’s no doubt about it, WordPress is a great blogging platform. It’s an incredibly flexible and multifunctional publishing platform and can even give a lot of fully-blown content management systems a run for their money.
That’s not to say there isn’t room for improvement. Currently the function which the_excerpt() template tag calls upon leaves a little to be desired. The excerpt is used in most themes for browsing the archives and categories of a blog. Rather than displaying the full content of the post, the excerpt displays a short snippet of the content. Unless you manually enter in an excerpt when writing each post, WordPress grabs the first 55 words of the post and uses that as the excerpt.
So far so good, but there are problems with the way WordPress does this. These include:
Word count - 55 words is a good number, but what if you want more or less?
Formatting - WordPress strips out all HTML tags. This gets rid of images and links, but can also get rid of paragraph formatting, making the entire excerpt one long paragraph without any line breaks.
JavaScript - Unfortunately JavaScript isn’t stripped out, which can result in some plugins’ messy script appearing in your excepts. Not only does this look rubbish, it can be a vulnerability too.
There are ways to fix these problems, and I will show you how. Better yet, these changes can be done completely within your theme without having to hack away at the WordPress core files.
Finding the excerpt function
The first thing we need to do is find the relevant excerpt function that is not doing its job very well. Actually, you don’t need to find it because I’ve done it for you, but if you insist you can find the code below deep within wp-includes/formatting.php.
function wp_trim_excerpt($text){// Fakes an excerpt if needed
Remember, we are not altering the core files here, so we need to copy this code and paste it into our theme files. Open up your functions.php theme file and paste the code in.
This function is called wp_trim_excerpt() but it is important that we call it something unique. I’m going to rename it improved_trim_excerpt() but you can call it what you want. Edit line one accordingly:
function improved_trim_excerpt($text){
Increase the word length of the excerpt
This is simple. Find line eight and change the value to whatever you want - lets say 80.
$excerpt_length = 80;
Include HTML tags
If you have problems with your formatting you may need to include the P tag. You may want to include links or even images. Find line seven and replace it with this:
The final thing we need to do is tell WordPress to use our new and improved function rather than its built in function. Fortunately this is a piece of cake and involves entering two more lines at the end of our functions.php theme file.
Brilliant, Aaron. That’s very useful.Very clearly explained too.
Which plugin do you use for your code snippets within your post? Very nice, what with the syntax highlighting and all. (It even has links to the functions on the php.net site!)
Johno - You know I actually wrote this article in July but didn’t post it until yesterday because I couldn’t get code to display properly in WordPress. The short answer to your question is that there are three plugins at play to get that code displaying properly - the main one being Hakan Carlstrom’s Code Viewer. The long answer to your question is coming up in a blog post.
Nice! I was looking for this exact post before I changed my theme. ^^
Have you got any idea if there is a plug-in that lets you display your archives and categories as titles and metadata only, which then expands to full view on click, without reloading?
I’m not aware of any such plugin. So let me get this straight: you want to show a list of article titles, and for the user to be able to click on the title, which reveals the full article without reloading, so presumably the user can then click another title and view that article, all without reloading the page?
Sounds like a bit of AJAX trickery would be needed to pull that off. I reckon I could do it by playing with the template files, but not sure I could make a plugin to do it. I haven’t quite sussed plugins yet.
Since most of my posts are relatively short, I reckon an archive like that would work — even though it might result in fewer comments, since people can read the posts without actually opening it independently.
I also imagine it would be a suitable solutions for bloggers who podcast, whose posts usually contain nothing more than a short paragraph, a download link and a player.
Thanks! That looks great. I’ll give it a try when I get back home. It’s probably heavy though, so I’ll have to buy increased memory usage. I’ve already got problems when adding images to posts etc.
[...] the scripts that pull in information from Amazon. A bit of poking around led me to a post on Improving WordPress’ the_excerpt() template tag. I followed the instructions in the “Finding the excerpt function” section to locate [...]
OUTSTANDING instructions … worked perfectly for our application of WordPress. Thank you. I wasn’t looking forward to digging around in the “core code” to override the 55 word limit. You’re very generous to share your hard work.
[...] per chi non vuole installare un ulteriore plugin e se la cava un pò con PHP esiste anche questo metodo. Articoli correlatiMa theme-viewer è [...]
Thank you for your interesting tutorial. Do you think there’s a way to set the excerpt length on a certain number of lines instead of a certain number of characters?
I mean lines as break-rows.
Alberto - I don’t think you can do that as web pages aren’t rendered in lines. You could split at character length, word length and then the next denomination would be paragraphs. I would suggest working out an average words per line and splitting accordingly.
John - I know - WordPress autmatically converts my quotes to curly quotes - it’s quite annoying. I’m still looking for a plugin to fix this.
Alberto - Regex makes me dizzy… :p
I still think it would be impossible to do lines because PHP will just see a string of text. You could split it at the end of a < P > tag but then some paragraphs are longer than others.
Rob - It’s not entirely clear what you’re having problems with, but in case this helps I don’t think you can use $variable = the_excerpt(); - Instead it should be $variable = get_the_excerpt();
Hi Aaron
Thanks for the code. Brilliant. One last thing though. Is it possible to add a class to the img tag so that it loads the img as a thumbnail. The image in the main post needs to be full size. Take a look at this page: http://www.lostwithiel.eu/category/businesses/shops/antiques-shops-businesses/
This is a list of shops within that category and I want the image tag to be small and link to the main post. Is this possible in this code please?
Thanks
Rich
Richard - I’m afraid there is no easy solution to that, although if you’re prepared to do a bit of research and try and work a solution I think it can be done one of two ways:
PHP is capable of resizing images on the fly using the GD Library. Do a search and there should be quite a lot of info. I’m not sure how you’d integrate it with my script above but it should be possible.
The second option is to manually upload a thumbnail of each image you use and then place a piece of JavaScript in the category template page that looks at the src attribute of each image and replaces it with its accompanying thumbnail.
Both solutions require getting your hands dirty but I think it can be done.
Mike - Did you just copy and paste the code from my site? If so, this might be the problem. Wordpress is converting each of my single quote marks to fancy curly quote marks. If you go through the code and change each ‘curly’ quote to a straight quote it should work.
I’m still on the hunt to find a plugin that fixes this!
What’s the purpose of the code below ?
array _ push($words, ‘[…]’);
I dislike the look of […] and it seems pretty much non functional. Can it be removed entirely, or is it needed for something?
Kathy - That line simple appends ‘[...]‘ to the end of the excerpt. You could miss out the line entirely if you don’t like it or alternatively replace ‘[...]‘ with ‘continued’ or whatever you like.
Excellent tips here! It greatly sped up the process of making the mods I wanted. Thank you!
In case anyone can use them, I reproduce below two other changes I made based on your example.
#1: Excerpts are 55 characters, but only excerpt the text if it has more than 75 characters. I always hate it when I have to load a new page for just the last word or two. (Of course you can change the thresholds as you wish.) Replace lines 9-11 with:
$excerpt_length = 55;
$excerpt_cutoff = 75;
$words = explode(’ ‘, $text, $excerpt_cutoff + 1);
if (count($words) > $excerpt_cutoff) {
$words = array_slice($words,0,$excerpt_length+1);
#2: Make the [...] into a link:
array_push($words, ‘[ >> more ]‘);
Hi Nick - Thanks for you contribution to imrpoving the excerpt further. Unfortunately WordPress has parsed the link in your second amend, but I’m hoping most people will be able to work it out. Thanks.
I tried this code, and all it does for me is completely lockup every page (including wp-admin) of my blog in a white screen. Once I removed the code from functions everything works fine again…
JT - if you copied and pasted the code directly it probably throws up errors as WordPress converts all my single quotes to curly quotes. Try and fix the quotes and it should work fine.
johno
3 September 2007, 7:15 am