How to Add Multiple Custom Excerpt Lengths In WordPress

By default WordPress excerpts are set to 55 words and there is an excerpt_length filter which allows you to change this default value to your length of choice. But what if you wanted a different excerpt length on different sections of your site? For example if you are developing a website with multiple post types you may want to show different excerpt lengths depending on the post type. A blog post may have a longer excerpt than for example a portfolio item.
Custom Excerpt Function
Below you will find a custom function we’ve developed to help you display excerpts at different lengths in WordPress. To use the function simply add it to your functions.php file and then you can go about replacing either the_excerpt or the_content functions throughout your theme so you can different excerpt lengths.
function wpex_get_excerpt( $args = array() ) {
// Default arguments.
$defaults = array(
'post' => '',
'length' => 40,
'readmore' => false,
'readmore_text' => esc_html__( 'read more', 'text-domain' ),
'readmore_after' => '',
'custom_excerpts' => true,
'disable_more' => false,
);
// Apply filters to allow child themes mods.
$args = apply_filters( 'wpex_excerpt_defaults', $defaults );
// Parse arguments, takes the function arguments and combines them with the defaults.
$args = wp_parse_args( $args, $defaults );
// Apply filters to allow child themes mods.
$args = apply_filters( 'wpex_excerpt_args', $args );
// Extract arguments to make it easier to use below.
extract( $args );
// Get the current post.
$post = get_post( $post );
// Get the current post id.
$post_id = $post->ID;
// Check for custom excerpts.
if ( $custom_excerpts && has_excerpt( $post_id ) ) {
$output = $post->post_excerpt;
}
// No custom excerpt...so lets generate one.
else {
// Create the readmore link.
$readmore_link = '<a href="' . esc_url( get_permalink( $post_id ) ) . '" class="readmore">' . $readmore_text . $readmore_after . '</a>';
// Check for more tag and return content if it exists.
if ( ! $disable_more && strpos( $post->post_content, '<!--more-->' ) ) {
$output = apply_filters( 'the_content', get_the_content( $readmore_text . $readmore_after ) );
}
// No more tag defined so generate excerpt using wp_trim_words.
else {
// Generate an excerpt from the post content.
$output = wp_trim_words( strip_shortcodes( $post->post_content ), $length );
// Add the readmore text to the excerpt if enabled.
if ( $readmore ) {
$output .= apply_filters( 'wpex_readmore_link', $readmore_link );
}
}
}
// Apply filters and return the excerpt.
return apply_filters( 'wpex_excerpt', $output );
}
How To Use The Function
So now instead of using “the_excerpt()” in your loops you can use “wpex_excerpt( $args )”, where $args holds an array of your preferred settings for the specific excerpt.
Example:
<?php echo wpex_get_excerpt ( $defaults = array(
'length' => 40,
'readmore' => true,
'readmore_text' => esc_html__( 'read more', 'wpex-boutique' ),
'custom_excerpts' => true,
) ); ?>
Function Filters
You may notice that our snippet includes various apply_filters() functions. Why? The reason for this is if you are working on a premium or free theme or plugin for distribution you will want to give the end users the ability to alter their excerpts via their child theme if needed. For example if you defined an excerpt to be a certain length for a section of the site the end user could always use the “wpex_get_excerpt_args” filter to change the excerpt to a different length or customize the text or readmore
FAN-TAS-TIC! thanks!
Hey keylime…There is actually a better way to do this. Have a look here:
http://www.wpexplorer.com/snippet/trimlimit-wordpress-content
I use this alot and it works on pages and post, because its a WordPress function so no need of writing another function.
Thanks! this is awesome! 🙂
My programming skill is so bad. But it help me to adding multiple excerpt 😉
Thanks so much AJ
Thank you,
life saver as usual!
Thanks for the wp_trim_words tip. Super handy, and new to me.
It’s also handy to know that you can control your titles by altering this code. It definitely helps when you only want to show the headline of a title and limit it for the client.
Helo AJ I’m developing wp theme and iterested if I can use this code snippet in commercial use? Thanks in advance 🙂
Definitely any tutorial on my site you can use as you see fit 😉
ok. many thanks 🙂
You rock dude, thanks!
Thanks for this AJ. I am having an issue with this code though as it wants to remove the image from the very top of the post (not set by featured).
How can I make it include images also?
Thanks
You can’t Jamie, because the wp_trim_words function strips all HTML. If you have a look at the function in the codex you’ll see that one of the first things the function does is run the content through wp_strip_all_tags.
Cheers for clearing that up AJ. Do you know any alternative methods I could use so it would leave the HTML in tact?
Thanks
Not via any core functions, the only alternative I can think of would be using PHP (maybe substr?)
Ok cheers anyway AJ. I’m no coder, so I will just carry on playing to see if I can come up with a solution!
You are the best!
thank yoo. Just what I was looking for. No idea how it works, but who cares 😀
tnx for this. is it possible to add “read more” link after trimming the text?
It works perfectly. Thankyou for for this interisting posting.
Hi there, I have been using this for a while and works great, is there a way to use this function with the get_post function, it does not seem to work when I try to use it, thanks
Great Post! Thank you!
Nice Post. Thanks for share..
line 21 should be :
$args = apply_filters( ‘wpex_get_excerpt_args’, $args );
Thanks for the heads up!