Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would one limit the Manual Excerpt Length in WP?

One can control the WP default (Automatic) excerpt length of a WP post using the using the following snippet within functions.php;

From the WP Codex

// . Post excerpt adjustment (Auto)
// . ==============================
function wpdocs_custom_excerpt_length( $length ) {
    return 20;
}
add_filter( 'excerpt_length', 'wpdocs_custom_excerpt_length', 999 );

My question is how do you limit the manual one?

You know, the exerpt specifically added by the user themselves?

(*) There is an 8 year old question here, that does provide some context but given the current year and progress WP has made I want to post the question again and receive some clarity on the subject.

Added Context: (Edited: 12 March 2019)

It's not that the original answer to the question posted earlier doesn't work, all be it seems, really clunky. I'm looking for a more simple & robust answer using exerpt_length filter. Rather than using something like the following to trim the text; (If Possible)

function excerpt($limit) {
    return wp_trim_words(get_the_excerpt(), $limit);
}
like image 448
Beaniie Avatar asked Mar 09 '19 17:03

Beaniie


People also ask

How do I trim an excerpt in WordPress?

While there's probably a plugin for this, we have created a quick code snippet that allows you to use wp_trim_words to trim your text in WordPress. WordPress 3.3+ has a core function called wp_trim_words() . This function will trim text to a specified number of words and return the result.

How do I remove read more from excerpt?

function child_theme_setup() { // override parent theme's 'more' text for excerpts remove_filter( 'excerpt_more', 'new_excerpt_more' ); } add_action( 'after_setup_theme', 'child_theme_setup' ); // Replaces the excerpt "Read More" text by a link function new_excerpt($more) { global $post; return '<a class="moretag" href ...

How do I show limited post content in WordPress?

Limiting the words or characters during the display can be done by either creating a function in the function. php file or right in the code itself using the WordPress function wp_trim_words() or mb_strimwidth() etc. There are two ways to display content using the_content() and get_the_content() function.


1 Answers

We have by default in core, the following filtering:

add_filter( 'get_the_excerpt', 'wp_trim_excerpt' );

but within wp_trim_excerpt() the trimming is only applied on the post's content, when there's no manual excerpt set.

Here's an untested suggestion for a custom filtering:

add_filter( 'get_the_excerpt', function( $excerpt, $post ) {
    if ( has_excerpt( $post ) ) {
        $excerpt_length = apply_filters( 'excerpt_length', 55 );
        $excerpt_more   = apply_filters( 'excerpt_more', ' ' . '[&hellip;]' );
        $excerpt        = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
    }
    return $excerpt;
}, 10, 2 );

to apply the similar trimming on manual excerpts.

Hope you can adjust this further to your needs.

like image 69
birgire Avatar answered Oct 12 '22 14:10

birgire