A Couple Of Simple Tricks For Tweaking WordPress Excerpts
Many blogs and other websites (like this one!) present a front page of teasers for multiple stories. These excerpts are intended to provoke interest and encourage readers to delve deeper into the site content. The default WordPress the_excerpt call makes it easy to implement automatic excerpts into a blog, but it’s not very flexible. Here are a couple of very simple tricks for tweaking WordPress excerpts.
Something different than ‘Read More’
Using the_excerpt in a WordPress theme truncates your post to a default of 55 characters and adds “…” (called an ‘ellipsis’) and a link to the main article with the text “Read More”. That’s fine, but it also can’t be changed. You might want to change that text to read “Read the full article”, remove the ellipsis, or add surrounding HTML and styling.
This flexibility can be added in the functions.php file. All we need to do is add a simple filter to the default function excerpt_more. I’m going to make three small changes.
- Replace the standard … with three actual periods (not exactly semantically correct, but I never like how … looks)
- Add a class to the “read more” link
- Update the text to ‘[read more]‘ instead of ‘Read More’
function new_excerpt_more($more)
{
return '... <a class="heavier" href="' . get_permalink($post->ID).'">[read more]</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');
That’s all that needs to be done. Now, whenever I call the_excerpt within my WordPress theme, the read more link will be formatted exactly as I want it.
Tweaking excerpt length
The WordPress default excerpt is 55 characters long, with the HTML tags stripped from it (so no bold text, no links, etc). There are plugins which give you significantly more flexibility and control over excerpt formatting (Advanced Excerpt is a great one), but I just want more control over the length of that excerpt.
The simplest way to do this would be to add a filter that just resets the excerpt length to whatever I want.
function new_excerpt_length()
{
return 30;
}
add_filter('excerpt_length', 'new_excerpt_length');
Now the default excerpt length when I call the_excerpt is 30 characters.
I’d like to do more though. I want to have different excerpt lengths in different parts of my template. 50 characters in my index.php, but 75 characters in my search.php. I could even set different excerpt lengths in different parts of the same page. E.g. a featured story might have a longer excerpt than other news.
To achieve this I’m going to add to the filter I’ve already created.
function new_excerpt_length()
{
global $customLength;
if($customLength)
{
return $customLength;
}
else
{
return 30;
}
}
add_filter('excerpt_length', 'new_excerpt_length');
This gives me the capacity to change the excerpt length wherever I call it in the template. In my index.php I do the following instead of simply calling the_excerpt.
<p><?php $customLength=50; echo get_the_excerpt(); $customLength=0; ?></p>
Couple of quick things.
I’m echoing get_the_excerpt instead of making a call to the_excerpt, so I’m wrapping the php in a <p> element (the_excerpt does this automatically).
I’m resetting the $customLength to zero after get_the_excerpt is called. I’m doing this because in the functions.php I set a default value of 30 for excerpt lengths. If the excerpt is called anywhere else on this page, it’ll use the default length now (or any new $customLength I set).
Simple tweaks add value
It’s always great to have more flexibility in design and content. Even the most minor changes can give a website a greater sense of uniqueness. Tweaking how a “read more” link is displayed, or how long a story excerpt is might not seem huge. For the user, however, such changes demonstrate an attention to detail which can help them feel like they’re not just on any other site.
Many aspects of design and content are appreciated on an almost subconscious level. Very small details, particularly added together, can have a strong positive impact on user experience.
If you enjoyed this post, why not subscribe to receive all the latest stories from Shiny Toy Robots?