Don’t Worry, You’re In The Right Place
A lot of these posts were originally on SureFireWebServices.com. So if you found them through search engines or clicking around, this is their new home :). Enjoy!
0

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 );
[/code]

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

[themedy_alertbox icon=”” colour=”yellow” font_awesome_att=”” custom_colour=””] 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. [/themedy_alertbox]

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’);
[/code]

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!)

Advertisement
About the Author
What's UP! This is my site, I write 99% of the articles on here. I'm also the owner of SureFireWebServices.com. I help out a lot of developers and designers getting into the web game. Helping is fun for me, so feel free to ask me any questions! I've made courses and have a membership as well to help get you on your feet!

0 comments on How to shorten / edit the manual excerpt in wordpress

  1. Corey Barnett says:

    Not getting the code to work man. Not sure what the problem is…

    1. 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!

      1. Corey Barnett says:

        Theme: FreshPick New

        Send me an email to talk more specifics. Thanks for your help.

Leave a Reply

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

Scroll to top