How to shorten / edit the manual excerpt in wordpress

So I had to figure this out for one of my clients and what a pain. I found a bunch of code out there that said they edited the length of the excerpts, but they didn’t. Then I did some research on the wordpress codex and found out some interesting things about the excerpt. THEN I finally found the code to shorten it!

The wordpress excerpt explained… kind of.

So basically what I found out was that there are three different types of excerpts that wordpress uses, the automatic excerpt, the manual excerpt, and the teaser.

Here’s the explanation directly from the WordPress Codex:

When a post has no manual excerpt and the post template uses the the_excerpt() template tag, WordPress generates an excerpt automatically by selecting the first 55 words of the post. When the post template uses the the_content() template tag, WordPress will look for the More tag and create a teaser from the content that precedes the More tag.

Here’s the kicker that’s not stated!
When you add a manual excerpt, it’s not affected by any of those tags!! What does that mean? It means that this code:

function custom_excerpt_length( $length ) {
	return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

… doesn’t work when you add a manual excerpt to a page!

[sws_yellow_box box_size="665"] Side Note: If you don’t know, the manual excerpt is the excerpt box that you find on your posts page in the admin. They can also be available when creating custom taxonomies. [/sws_yellow_box]

Shorten the manual excerpt

Add this to the functions.php file in your template:

function wp_trim_all_excerpt($text) {
// Creates an excerpt if needed; and shortens the manual excerpt as well
global $post;
  $raw_excerpt = $text;
  if ( '' == $text ) {
    $text = get_the_content('');
    $text = strip_shortcodes( $text );
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
  }
 
$text = strip_tags($text);
$excerpt_length = apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); //since wp3.3

return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); //since wp3.3
}
 
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wp_trim_all_excerpt');

This works and it works awesome!

Resources:
http://codex.wordpress.org/Excerpt
http://www.transformationpowertools.com/wordpress/automatically-shorten-manual-excerpt (for the code that saved me soo much time!)

Comments

    • Jonathan says

      Where are you putting it? There are a lot of things that can conflict with the code like plugins and themes. I was having an issue with a woo commerce plugin at one point because of the way it does things. What theme are you using? Can you provide some details? Thanks!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>