Limiting the Length of an Excerpt in WordPress

In WordPress, there are two ways to display a summary of a post that I have found so far.  Depending on your scenario you can use either php the_content(‘Read more…’); ?>  or . I usually end up using both depending on the page that is being displayed and simply choose which with a “if, else” statement.  In most scenarios it is better to use the excerpt function but the default character length is either too short or too long.  The bad news for you is that there is no way to change this in WordPress without modifying some code.  The good news is that you can do this by simply modifying your theme’s functions.php file.

Excerpt:
An excerpt is a condensed description of your blog post and refers to the summary entered in the Excerpt field of the Administration > Posts > Add New SubPanel. The excerpt is used to describe your post in RSS feeds and is typically used in displaying search results. The excerpt is sometimes used in displaying the Archives and Category views of your posts. Use the Template Tag the_excerpt() to display the contents of this field. Note that if you do not enter information into the Excerpt field when writing a post, and you use the_excerpt() in your theme template files, WordPress will automatically display the first 55 words of the post’s content.

An excerpt should not be confused with the teaser, which refers to words before the in a post’s content. When typing a long post you can insert the 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 version of your post. The Template Tag, the_content() should be used to display the teaser.

1. Edit your theme’s functions.php file
2. Paste the following code between the <?php and the ?>

// --------------------------------------------------------------------------
// Start Excerpt Length Change
// --------------------------------------------------------------------------
	function new_excerpt_length($length) {
		return 150; // change this to your liking
	}
	add_filter('excerpt_length', 'new_excerpt_length');

	function new_excerpt_more($post) {
		return '<a href="'. get_permalink($post->ID) . '">' . ' Read More...' . '</a>';
	}
	add_filter('excerpt_more', 'new_excerpt_more');
// --------------------------------------------------------------------------
// End Excerpt Length Change
// --------------------------------------------------------------------------

In the example above, I have set the excerpt length to 150.

Note: These functions are not of my own creation, simply things I have found along the way.  If you are the author of this function, please let me know so I can properly credit you with your work!

About Joe D

I have always had a passion for everything computing. In early 2000, I decided to take my passion to the web. Thus, C.O.D. was born. Through the years we have made many great friends at C.O.D. and hope to continue our journey for years to come.

Check Also

Manage Multiple WordPress Sites with InfiniteWP

Running your website, or websites, on WordPress is an easy choice as the flexibility the …

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.