How To Improve WordPress Excerpt Without a Plugin

In this tutorial I will show you how to enhance WordPress Excerpt by using simple functions and without the need of a plugin. WordPress Excerpt is an optional summary of your content.

The relationship between the Manual Excerpt, Automatic Excerpt, and Teaser is this: When a post has no manual excerpt and the post template uses the the_excerpt() tag, WordPress automatically generates an excerpt by selecting the first 55 words of the post followed by the unlinked ellipsis “[...]”. When the post template uses the the_content() tag, WordPress will look for the More tag and create a teaser from the content that precedes the More tag.

In WordPress, the_excerpt() template tag displays the excerpt of the current post with an un-linked ´[...]´ at the end. If you do not provide a manually written excerpt to a post, it will display an automatic excerpt which by default refers to the first 55 words of the post´s content. Also in the latter case, HTML tags and graphics are stripped from the excerpt´s content.

What about a Plugin? Yes, there are plugins that customize WordPress auto-generated excerpt. But haven´t you read my previous post about the disadvantages of going crazy adding plugins to your theme? In addition, aren´t you eager to learn and do things yourself?

How To: Change the Excerpt Length

From WordPress Version 2.8.0 and above you can change the excerpt length from the default 55 words. Open the functions.php file located in your current theme´s folder and add (copy and paste) the following code.

Code1 (Valid for WordPress Version >= 2.8.0)

<?php 
function custom_excerpt_length( $length ) {     
	return 35; //Change this number to any integer you like.
}
add_filter( 'excerpt_length', 'custom_excerpt_length' );
?>

References:

How To: Change the Excerpt More String

By default, the excerpt more string at the end is set to ´[...]´. To change the default excerpt more string, add the following code to your theme´s functions.php file:

Code2 (Valid for WordPress Version >= 2.9.0)

<?php 
function custom_excerpt_more( $more ) {     
return ' ...'; // nicer without the brackets, but not very useful. 
}
add_filter( 'excerpt_more', 'custom_excerpt_more' );
?> 

Code2a (Valid for WordPress Version <= 2.8.x)

<?php 
function custom_excerpt_more( $excerpt ) {
	return str_replace( '[...]', '...', $excerpt );
}
add_filter( 'wp_trim_excerpt', 'custom_excerpt_more' );
?>

References:

How To: Replace the Excerpt More String with a Link

By default WordPress outputs an un-linked “[...]” at the end of each excerpt, which is not useful for Accessibility and SEO purposes. Open your theme´s functions.php file, and add the following code.

Code3a (Valid for WordPress Version >= 0.7.1)

<?php 
function custom_excerpt($text)   
{   
	return str_replace('[...]', '<a href="'. get_permalink($post->ID) . '">' . '&raquo; Continue Reading.' . '</a>', $text); 
}   
add_filter('the_excerpt', 'custom_excerpt');  
?>

If you want to add the post title to your link in the excerpt, then add the following code to your theme´s functions.php file.

Code3b (Valid for WordPress Version >= 0.7.1)

<?php 
function custom_excerpt($text)   
{   
  return str_replace('[...]', ' &#8230;<br /><a href="'. get_permalink($post->ID) . '">' . 'Continue Reading: '. get_the_title() . '</a>', $text);  
}   
add_filter('the_excerpt', 'custom_excerpt');  
?>

If you are using WordPress Version 2.9 and above you can use the following code.

Code3c (Valid for WordPress Version >= 2.9)

<?php
function custom_excerpt_more($more)   
{  
   return ' <a href="'. get_permalink($post->ID) . '">' . '&raquo; Continue Reading.' . '</a>';
}   
add_filter('excerpt_more', 'custom_excerpt_more');  
?>

If you want to add the post title to your link in the excerpt, add the following code to your theme´s functions.php file.

Code3d (Valid for WordPress Version >= 2.9)

<?php
function custom_excerpt_more($more)   
{  
  return  ' &hellip;<br />' . '<a href="'. get_permalink($post->ID) . '">' . 'Continue Reading: '. get_the_title() . '</a>'; 
}   
add_filter('excerpt_more', 'custom_excerpt_more');  
?>

WARNING: You can ONLY add to your theme´s functions.php, Code3a, or Code3b, or Code3c, or Code3d, but not all or any combination.

References:

How To: Change Excerpt Length Depending on the Category

If you want to modify the excerpt length based on the category, then add the following code to your theme´s functions.php file. Don´t forget to change the category ID to yours.

Code4a (Valid for WordPress Version >= 2.8.0)

<?php
function custom_excerpt_length($length) {
    if (in_category(4)) {  //For posts belonging to category ID 4
        return 65;
    } else {
        return 40; //for all others, return 40 words.
    }
}
add_filter('excerpt_length', 'custom_excerpt_length');
?>

If you want to modify the excerpt length based on multiple categories, then add the following code to your theme´s functions.php file instead. Don´t forget to change the category IDs to yours.

Code4b (Valid for WordPress Version >= 2.8.0)

<?php
function custom_excerpt_length($length) {
    if(in_category(array(4,40))) {  //For posts belonging to category IDs 4 and 40
        return 65; //return 65 words for the excerpt
    } else {
        return 40; //for all others, return 40 words.
    }
}
add_filter('excerpt_length', 'custom_excerpt_length');
?>

WARNING: You can ONLY add to your theme´s functions.php, Code1, or Code4a, or Code4b, but not all or any combination.

If there is a More tag in your post, then the excerpt word count stops at either where the More tag is placed, without the excerpt trailing characters, or your defined excerpt length, whichever comes first. Therefore make sure that you place the More tag outside your specified excerpt word count.

If you don´t know how to find the Category ID, then read my previous tutorial.

Troubleshooting

If you are having problems with any of the code blocks above, please note the following:

  1. The filters only work on excerpts that are automatically generated by WordPress. The filters do not affect the manually typed excerpts.
  2. The filters only work on excerpts that use the_excerpt() template tag.
  3. Check your plugins. Some plugins contain code that overrides the excerpt.

References:

How To: Replace the_content() With the_excerpt

Never show full posts in the main page. In your theme, If you have the_content() template tag, make sure to use the More tag in your post. They work together, otherwise your posts will be shown in full in the main page which can dramatically slow page loading speed. For example let´s say you display 8 posts in your main page, and by showing every post in full, then it will take long time for the page to load and for the user to scroll. One way to reduce page load time, is to replace the_content() template tag with the_excerpt() template tag.

To do that, open the index.php file located in your theme´s folder and replace the the_content() with the_excerpt(). Also you could do the same for archive.php and search.php files. (DO NOT TOUCH page.php or single.php files.)

Code5

//The '...' can be anything inside. 
//Search and Replace the_content() template tag
<?php the_content('...'); ?>

//With the_excerpt() template tag
<?php the_excerpt('...'); ?>

NOTE: the_excerpt() template tag has no parameters. However, there is no harm from keeping them in case you wanted to revert back and use the_content() again. If you get any errors, then replace the_content(‘…’); with the_excerpt();.

To EMPHASIZE: the_content() template tag is used in conjunction with the More tag. Make sure to place the More tag somewhere at the beginning of the post. Otherwise, the whole content of the post will be displayed.

Reference:

How To: Manually Add a Post Excerpt

To manually add an excerpt to a post, you need to login to your WordPress dashboard, find your post, and add the excerpt as shown in the picture below:

Manually adding an excerpt in WordPress dashboard.

The Advantage of a Manual excerpt is that they are often used by search engines to describe search results. In this CASE, good excerpts increase and improve Website traffic. Also, WordPress preserves HTML formatting in manual excerpts which is not the case for the automatically generated excerpt. Another major drawback of the automatic excerpt is that the beginning of a post is usually an introduction and not a summary.

When using the manually typed excerpt feature, the ´[...]´ is not automatically added nor a link to a page containing the full post. However, to bypass this shortcoming you need to replace the_excerpt(); in your theme´s index.php (and also in your theme´s archive.php and search.php files) with the following code below. The beauty about this code is that it takes care of both scenarios: the manually added and the automatically generated excerpts; which means that you can have both manual excerpts and auto-generated excerpts for your posts with no issues.

Code6

//In your theme files: Search and Replace the_excerpt() template tag
<?php the_excerpt(); ?>

//With the following code:
<?php 
//Add Links to Manual and Automatic excerpts
if ( $post->post_excerpt ){ //If manual excerpt is added
    //Add a link at the end of the manual excerpt 
    the_excerpt();?>
    <p><a href="<?php the_permalink(); ?> "> &raquo; Continue Reading: <?php the_title() ?></a></p>
<?php }
else {//else, it is an automatic excerpt, then let the filter handle it.
     the_excerpt();
     }
?>		

How To: Only Display a Post´s Excerpt if it Exists

If you only want WordPress to display a manually added excerpt and you don´t want WordPress to automatically generate any excerpts for you, then in your theme files (index.php, archive.php and search.php), then follow the instructions in the code below:

Code7

//Replace the_excerpt() template tag 
<?php the_excerpt(); ?> 

//With this:
<?php if ( $post->post_excerpt ) the_excerpt(); ?>

General Warning

When you add several PHP code blocks in your theme´s funtions.php file, make sure that you don´t leave any whitespace (spaces, newline) before the opening PHP tag or after the closing PHP tag. Like so (the correct way):

<?php 
//Some Code here beetween the opening PHP tag (above) 
//and the closing PHP tag (below)...
?>
<?php 
//Some other Code here ...
?>

In the above code, if you leave any whitespace or a newline between lines 4 and 5, you will get the following error: Warning: Cannot modify header information – headers already sent by (… , in your login screen and after you login to your WordPress dashboard.

General Advice

Keep WordPress Up to Date. If you can, you should always have the latest version of WordPress. Upgrading to the latest version is necessary because upgrades usually include performance and security enhancements and also new features. Go and download the latest version.

Excerpt Definition Revisited

The manually typed excerpt is a short description of your post typed in the Excerpt field in your Administration Panels. The excerpt is used to describe your post in RSS feeds and is typically used in displaying posts summary in the main page, in search results, in archives, and category views. You must use the the_excerpt() template tag to display the contents of this field. If you do not type any information in the post´s Excerpt field, and you use the_excerpt() in your theme files, then WordPress will automatically generate the Excerpt of your post.

The excerpt should not be confused with the teaser. When typing a long post you can insert the <!--more--> Quicktag after a few sentences to act as a cut-off point. When the post is displayed, the teaser, followed by a hyperlink (such as Read the rest of this entry…), is displayed. Your visitor can then click on that link to see the full content of your post. the_content() template tag should be used to display the teaser.

Conclusion

These simple functions, help you enhance the default WordPress excerpt without the need of a plugin. Do you have any other suggestions? or anything else to say? If so, please share your opinion in the comments section. Your opinion matters, unless it is a Spam.

As you already know by now, the automatically generated excerpt is stripped from all graphics and HTML tags. In my next tutorial, I will discuss how to keep HTML formatting in the excerpt (and you get to choose which tags to include), so go ahead and read it!

General Reference

If you found this post useful, please consider: linking back to it, subscribing by email to future posts, or subscribing to the RSS feed to have new articles delivered to your feed reader, or feel free to donate. Thanks!

About the Author |
Boutros is a professional freelance Web designer, Web developer, Software Engineer, and Blogger. He is the Chief developer at Blue Olive Online. He strives for pixel perfect design, clean robust code, and user-friendly interface. If you have a project in mind and like his work, feel free to contact him. Connect with Boutros on StumbleUpon, Twitter, and LinkedIn.
Visit Boutros AbiChedid Website.

42 Responses to “How To Improve WordPress Excerpt Without a Plugin”

  1. Jason says:

    Maybe you can help. I like having manual excerpts, don’t always want to use the first 55 characters, but we just changed to a new theme where larger excerpts clash with the design. We have a rather large site (over 1800 posts) so I don’t want to have to go into each one and alter the size. Is there a way to allow manual excerpts but limit the lenght. Preferably without having to edit functions.php. Other files are okay, but one false step in that one and everything goes down.

  2. Mariusz - trojmiasto.us says:

    Hi,
    I wonder how to display elements in excerpt as a list:

    <ul><li>...

    Something like parameters in the_tags would be helpful, but does not work:

    the_excerpt('<ul><li>','</li><li>','</li></ul>');

    Any ideas?

  3. Rahul Kashyap says:

    Really Nice tutorial Thanks for sharing this post. very useful and valuable post:)

  4. Tony says:

    Hi
    I’d like to limit the number of words of a manually inserted excerpt. If you use the excerpt_length filter it only applies to the auto-generated excerpt and not to the manual one.
    Is there any way, without using a plugin and without changing the ‘the_excerpt’ function or wrapping it in a substr function?
    Thanks.

  5. batuli@developer says:

    Hi
    I am using inline post plugin to publish post in specific pages(one post in one page and other post in other pages.)THe plugin worked for as as per my requirements. But all the content of the post is displayed in that page while i wanted only to display excerpts only. I tried with and without any plugin. But none of these worked for me. I am using twentyten theme
    Please help me with this.

    Thanks