Site logo

Archived topic

Limit for post excerpt lenght

7 replies · Started by anabella on September 4, 2020

Viewing posts 1–8 of 8

Hi,

I´m building the posts archive using Elementor (posts widget), and I want to limit the excerpt length.
The widget has a "excerpt length" option, but it doesn´t work on manually created excerpts (my blog posts have those)

This is what Elementor explains: "When Elementor displays a post, if it has no excerpt, Elementor automatically generates an excerpt from the post’s content. The Excerpt Length option allows you to control the length of this auto-generated excerpt. However, the excerpt length only applies to posts in which no manual excerpt has been entered. The length specified in the Elementor widget is ignored if a manually-generated excerpt exists for a post. "

So, basically, now my blog page displays everything that is inside the excerpt, not matter how long that is.

I went to Generatepress > Customizer > Layout > Blog > excerpt word count > but this is not being applied either.

Any ideas? Any code I could add?

Hi there,

WordPress can only trim automatic excerpts. So Excerpt length settings whether it be in the Theme or in another plugin will not change them.

You can try adding this PHP Snippet:

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', ' ' . '[…]' );
        $excerpt        = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
    }
    return $excerpt;
}, 10, 2 );

I tested it on the GP blog - and it does work with Manual excerpt ie. The themes excerpt count will apply. Not sure if it will work with Elementor though.

Thank you su much David!

I added your code, and now I can control the excerpt length through the Elementor Widget. Interesting!

The only thing I´m missing is the dots [...] where the text is cut.
Any way of adding this as part of the code you sent me ?

Thanks a lot!

In the original code you will see […] which is the Horizontal Ellipsis - it displays correct on the GP Blog - i can only assume Elementor is removing that. What if you don't set Elementor to trim the words and try the GP excerpt length?

Thanks David. If I don´t set Elementor to trim the words (meaning, if I leave the Excerpt Length
field blank in Elementor) , the excerpt doesn´t show at all.
However, I added this code and it looks good enough:

.elementor-post__excerpt p:after {
content: ' ...';
}

Thank you so much for your help David. Your replies are always extremely helpful.

Glad to be of help :)

Hi David, I just switched to GeneratePress and I'm staging my site in order to gain speed. I choosed the Scribe layout.

I have the same problem... I would like to trim the excerpt in order to correctly align the "read the article" buttons.

Buttons not aligned...

Do I have to use your code? If so, where do I have to put it?

Thanks in advance! 🤗
Christiano

Hi Christiano,

We can try to align the buttons without the need to trim.

Can you start a new topic and share the link to the page in question?

This archived topic is closed to new replies.