Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Excerpt length don’t work on frontend

  • Hi,
    I would like to trim the excerpt of my query loop in the front page, but, although in the editor looks fine, in the frontend, the excerpt is fully shown.

    I read about issue for custom excerpt, and I am not sure what those are, but my posts are just regular posts (post format standard) and the excerpt is just the standard excerpt field.

    Following this I added the custom_excerpt class and the php code to the website. Still not working… Maybe I am doing something wrong…

  • Hi there,

    ‘Custom excerpts’ usually refer to the excerpts manually added inside the post Excerpts metabox. Is that where you added the excerpt?

  • Yes, indeed. I just though it was something I didn’t do.
    Anyway, the resource I found and the solution I tried, shouldn’t be already the one I should use?
    I added the custom class custom_excerpt and then added the following snippet to the website:

    add_filter('generateblocks_dynamic_content_output', function ($content, $attributes, $block) {
        if (
            !is_admin() && !empty($attributes['className']) &&
            strpos($attributes['className'], 'custom-excerpt') !== false
        ) {
            $custom_excerpt = get_the_excerpt();
            $limited_excerpt = wp_trim_words($custom_excerpt, 20, '');
            $content = $limited_excerpt . '→';
        return $content;
    }, 10, 3);
  • Remove that snippet and try this one instead:

    add_filter( 'get_the_excerpt', function( $excerpt, $post ) {
        if ( has_excerpt( $post ) ) {
            $excerpt_length = apply_filters( 'excerpt_length', 20 );
            $excerpt_more   = apply_filters( 'excerpt_more', ' ' . '→' );
            $excerpt        = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
        return $excerpt;
    }, 10, 2 );
  • Thank you, it’s now working, but the length is the one I put in the editor, right?
    I mean, the 20 in the $excerpt_length = apply_filters( 'excerpt_length', 20 ); is not taking into considerations, instead, the number i put in the field in the editor is what counts.

  • The excerpt length that you put inside the editor (I assume under the dynamic data setting) will only work for the posts that have no custom excerpts applied.

  • Then is odd, since the digits I put in the dynamic data setting is the one that actually change the length.
    Attached you will see screenshots of:
    1. Gutenberg editor with custom excerpt (all my posts have a custom excerpt)
    2. Code with the snippet you gave me, where 20 should be the length of the excerpt rendered
    3. The actual listing, where 5 words is the excerpt length, the length I put in the dynamic data setting of the block

    Post excerpt
    code snippet

  • Hi there,

    can you share a link to your site where i can see the issue ?

  • Sure, here’s the website:
    In the privat info, a temporary link to login as admin.
    The “issue”, is appearing in the homepage, in the query loop.

  • This is what i see on the front end:

    Is that correct?

    NOTE: What you see in the editor is not always 100% accurate as we have to generated dynamic content differently in the editor

  • That is correct. What is odd is that the number 20 I put in the snippet is ignored. Instead, the length of the excerpt is taken from the dynamic data setting.

    Alvind said:

    >The excerpt length that you put inside the editor (I assume under the dynamic data setting) will only work for the posts that have no custom excerpts applied.

    My posts have all custom excerpt and regardless what Alvind said, the actual number I use in the dynamic data setting is what counts, not the one in the snippet, which it should be the one that counts.

    I am just puzzled on what is happening, but it is actually working.

  • Ah – see here in Alvinds code:

    $excerpt_length = apply_filters( 'excerpt_length', 20 );

    What this is doing is setting the excerpt length to a value of 20 BUT its passing via the excerpt_length filter hook.

    BUT other functions also write a value to the excerpt_length
    Such as the query loop excerpt lenghth
    Or the Themes customizer exerpt length which you can see is getting applied to the single post because of that PHP Snippet.

    To stop the snippet from affecting the single post change it to:

    add_filter( 'get_the_excerpt', function( $excerpt, $post ) {
        if ( has_excerpt( $post ) && !is_single() ) {
            $excerpt_length = apply_filters( 'excerpt_length', 20 );
            $excerpt_more   = apply_filters( 'excerpt_more', ' ' . '→' );
            $excerpt        = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
        return $excerpt;
    }, 10, 2 );
  • Thank you David, but still, there is something working when it shouldn’t.

    I used the snippet you gave me (it’s in a mu-plugin), and edited the length to 5 (just to be sure to notice the difference).
    I then oper the homepage, where there is a listing of my posts, and the excerpt is more than 5 word long. So I check the query loop block in Gutenberg, and the excerpt length is set to 25 words, which is the length I see in frontend.
    I then went to the blog page, where I use an element to show a query loop. In that element, inside the excerpt block, I set the length to 45 word, and in frontend it shows 45 words.

    Where it is supposed to show 5 words?

    I am not complaining, I am able to chenge the excerpt length, and that’s what I need, but I am trying to understand what happened.

  • If you want to force it to show 5 words then the snippet would need to change to this:

    add_filter( 'get_the_excerpt', function( $excerpt, $post ) {
        if ( has_excerpt( $post ) && !is_single() ) {
            $excerpt_length = 5;
            $excerpt_more   = apply_filters( 'excerpt_more', ' ' . '→' );
            $excerpt        = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
        return $excerpt;
    }, 10, 2 );

    This will make ALL excerpts except on the single post to 5 words.

    Previously the code was this:
    $excerpt_length = apply_filters( 'excerpt_length', 20 );

    And that tells it to use the ‘excerpt_length’ with a value with 20.
    But the because the ‘excerpt_length’` is FILTER HOOK its value is getting updated via the settings in the query loop as it too uses that filter.

  • Thank you David,
    I think I got a better grasp on it!

    I close the ticket,
    Have a nice one!

  • Glad to hear that!

Viewing 16 posts - 1 through 16 (of 16 total)
  • You must be logged in to reply to this topic.