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:

[code language=”php”]
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:

[code language=”php”]
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: (for the code that saved me soo much time!)

Focus on what matters!

Focus on what matters!

No more wasting time trying to figure out how to code it all!  Sign up below and I'll share my tips and tricks on how to build websites fast, professionaly, and with minimum code!

You have Successfully Subscribed!