Changing the the_excerpt() template tag of WordPress
August 16, 2008 by admin
Filed under Domain Development, Domain News
If you downloaded my blog promotion and traffic building package Blog Adrenaline, you are probably on your way to creating your own blog theme by now. One of the bonuses I provide with all the blog traffic building tips in the ebook is a guide to building your own WordPress theme in addition to the free skeleton WordPress theme (another bonus). I actually built the DNXpert WordPress theme using the skeleton WordPress theme and theme building guide revealed in the Blog Adrenaline.
Anyways, continuing on the note of constantly improving the look and feel of DNXpert.com, I came upon an interesting problem - and solved it so I thought I would share.
To boost DNXpert.com’s SEO score I wanted to show a full blog post in only one place - at the exact blog post’s address. Everywhere else, including the DNXpert.com homepage and the archives and categories, I wanted to show only an excerpt of the original post. I wanted to do this to avoid any duplicate content penalties that may be incurred as a result of WordPress’s default configuration.
Additionally, I wanted to customize the number of words shown in the excerpt, change the default display of an excerpt and finally replace the default [...] that comes at the end of an excerpt with a link that says READ MORE.
To accomplish this, I first edited the archives.php, archive.php, index.php and search.php template files found in the dnxpert theme directory as explained in the Blog Adrenaline theme editing guide. I looked for the code the_content() and replaced it with the_excerpt(). Very quick and simple. If you do the same and open your blog homepage or category archive, you will now notice that your full posts are replaced with short excerpts, 55 words long by default with [...] marking the end of each excerpt.
Ok, now the duplicate content penalty is avoided, we can move on to customizing the actual excerpt.
Customization of all WordPress template tags when writing or modifying a theme should be done by customizing functionality in your themes’ functions.php file (DNXpert.com functions.php file is located in the DNXpert theme directory) and telling WordPress to use your customized functionality instead of the inbuilt one.
By default, the WordPress excerpt is formated by the wp_trim_excerpt() function found in wp-includes/formatting.php. To modify this functionality I copied the content of this function and pasted it into my functions.php file in my theme directory. (Note: it is extremely important to only edit files in your theme directory as everything else is overwritten whenever you upgrade your blog).
The default wp_trim_excerpt function looks like this:
function wp_trim_excerpt($text) { // Fakes an excerpt if needed
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text);
$excerpt_length = 55;
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words)> $excerpt_length) {
array_pop($words);
array_push($words, '[...]‘);
$text = implode(’ ‘, $words);
}
}
return $text;
}
As you can see from above, this function calls strip_tags($text) to remove all html fomatting from the excerpt - this why all excerpts lose formatting when displayed. The function also sets excerpt length to 55 words via $excerpt_length = 55; and finally the function appends [...] to the end of an excerpt via array_push($words, ‘[...]‘);.
So, in order to modify some of the functionality of this function, I created a my_wp_trim_excerpt() function in my theme’s functions.php file.
function my_wp_trim_excerpt($text) { // Fakes an excerpt if needed
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text, '<p>');
$excerpt_length = 50;
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, ' ... <a href="'. get_permalink() . '">READ MORE</a>');
$text = implode(' ', $words);
}
}
return $text;
}
As you can see from above, in addition to setting excerpt length to 50, I also made the strip_tags function not strip paragraph tags ( I want my excerpt to look nice ) by passing <p> as a parameter to the strip_tags function. You can append other tags to <p> if you wish for them to not be stripped either.
Finally, I replaced the ugly [...] with a cool READ MORE link with the following line of code:
array_push($words, ' ... <a href="'. get_permalink() . '">READ MORE</a>');
Ok, before you rush and try and do this yourself, there is one last thing that needs to be done. We need to tell WordPress to use my new my_wp_trim_excerpt function instead of the in-built wp_trim_excerpt function. How do we achieve this? By simply placing the following two lines in our functions.php file:
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'my_wp_trim_excerpt');
That’s it, we are done!

















Hi is it possible to use a fake excerpt as meta description between head /head?
I try to so this, but ..
do you know a solution for this?
thanks a lot
This is great tip. I have happily used this code (with a little modifications) on my site. Thanks for sharing…
@Monika: not sure, have you tried some of the SEO plugins already available?
@Guru: I am glad you liked it.
Hi I know that there are good SEO plugins for WP but if it is possible I wouldn’t like to use them.
Every SEO has its strategy to win
and the plugins offers the strategy of the plugin author.
I’ll found a work around - I’m using an extra loop to do this and now it works perfect for me
kindly regards
Monika
Thanks for this. My blog entries are beginning to look huge. Need to make them shorter….