-
g.lanzi
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.
EDIT:
Following this https://generate.support/topic/excerpt-length-in-gb-query-loop-block/#post-88194 I added thecustom_excerpt
class and the php code to the website. Still not working… Maybe I am doing something wrong… -
Alvind
Hi there,
‘Custom excerpts’ usually refer to the excerpts manually added inside the post Excerpts metabox. Is that where you added the excerpt?
-
g.lanzi
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 classcustom_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);
-
Alvind
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 );
-
g.lanzi
Thank you, it’s now working, but the length is the one I put in the editor, right?
I mean, the20
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. -
Alvind
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.
-
g.lanzi
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
-
David
Hi there,
can you share a link to your site where i can see the issue ?
-
g.lanzi
Sure, here’s the website: https://www.volevotutto.it/
In the privat info, a temporary link to login as admin.
The “issue”, is appearing in the homepage, in the query loop. -
David
This is what i see on the front end:
https://app.screencast.com/heQZCdMpvLuf5
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
-
g.lanzi
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.
-
David
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 );
-
g.lanzi
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.
-
David
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. -
g.lanzi
Thank you David,
I think I got a better grasp on it!I close the ticket,
Have a nice one! -
David
Glad to hear that!
- You must be logged in to reply to this topic.