Words and more attributs doesn't work for excerpt

Hello,

I would like to limit the number of words of my excerpt and add “…” at the end.

I have read the documentation and I believe that could be work with these two attributes: “words” and “more”… But it’s not!!! :laughing: I’m sure I’m close, but I need help…

I’ve tried the following code:

<Field excerpt auto=true words=25 more />

Should the word count be a string or a number?
And should I use just “more” (the default […] is ok for me) or “more”="…"?
Anyone of those solutions works. What i’m missing please ?

Additionally, I saw we can limit the number of characters whit this code :slight_smile:

          <Format length=125>
            <Field name />
          </Format>

Is there a similar method to limit the number of words of the feeld ? And can i use the attribut more on the Field ?

Thanks for your reply.

Hi there!

Maybe you can try this one to add “…” at the end
<Field excerpt auto=true words=25 more=true />

Thanks for this idea, but it’s doesn’t work neither !

Hi there, this should work…

<Field excerpt auto="true" words="25" more="..." />

Thanks Rips, but unfortunatly it’s steel don’t work…
I use the last version of wordpress 6.3… Maybe something doesn’t match with block theme ?

I found part of the solution by adding this function in my functions.php

add_filter( 'get_the_excerpt', function( $excerpt, $post ) {
     
    $excerpt_length = 30; // Change excerpt length 
    $excerpt_more   = '...<br><br><a href="' . get_permalink($post->ID) . '"></a>'; // Add ellipsis and 'Read More' permalink text when trimmed
     
    if ( has_excerpt( $post ) ) {
        $excerpt = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
    }
     
    return $excerpt;
     
}, 10, 2 );

But the <Field excerpt auto=true /> still don’t work…

I keep looking

I just did a test with the previous example and it works on WordPress 6.3, however I use my own theme. L&L might have some compatibility issues with Block Theme as this is a new and experimental architecture. :slight_smile:

I just did a test with the previous example and it works on WordPress 6.3, however I use my own theme.

Thanks for this test Phil Rips. I use a theme child from Twenty Twenty Three theme… So it’s should works… I keep searching what’s going on !

1 Like

I can confirm that <Field excerpt auto=true words=10 /> works with Astra. I think @Rips is on to something about the block themes.

Seems like there’s been a lot of helpful answers in this thread! I figured I’d chime in and share some clarifications based on my own testing.

For most attributes, L&L handles things when it comes to figuring out what’s a string and what’s a number. Adding quotes around it or not doesn’t change the way L&L understand an attribute, it’s seen as the same thing.

I don’t think the default noted in the docs actually applies. I’m still not clear on exactly why that more attribute exists and I haven’t been able to get a clear answer from a dev. Here’s some discussion on the matter from a previous thread. To answer your question though, it seems in the current release that you do have to explicitly define it if you want it to get displayed with more="...".

Personally, I’d just do this manually with something like

<Field excerpt auto=true words=25 />... <a href="{Field url}">Read more</a>

There are a bunch of new features of the `Format tag that were release recently, but that haven’t yet been documented. I’m working on getting the information I need from the dev team to be able to properly document that. In any case, keep an eye on the docs page for that tag (or just play around with the stuff mentioned in the changelog) because there’s some interesting stuff in that tag that you could probably use to achieve what yo’re looking for.

No, unfortunately. To keep the language concise and intuitive, formatting is always done with the Format tag (other than in rare cases where specific functionality is desired, like this excerpt field).

I just tested the following template in WordPress 6.3.1 running Twenty Twenty-Three theme and it worked as expected.

<Loop post_type=post count=5>
  <p><Field excerpt auto=true words=10 more="..." /></p>
</Loop>

Which results in the following output, as expected.

It seems there’s some customization on your site that’s causing this issue that doesn’t exist with a vanilla 6.3+ block theme installation. Maybe try disabling plugins or theme modifications to see if you can identify what’s causing the issue. I’m not seeing any incompatibility on my end.

Hello,
Thanks for your reply Benjamin :slight_smile:

Unfortuntaly, the version I was working crashed down, so I started with a blank theme generate by Create Block theme. I’ve instaled no plugins except ACF and tangible.
My function.php file doesn’t contain anything about excerpt… And it still doesn’t work… Really weird!

Can we fill the excerpt with a specific ACF personalize field ?

Really weird indeed! I haven’t heard of this happening to anyone else. Skimming the thread, I noticed that you’ve mentioned a few times that it isn’t working, but you haven’t expressed what exactly isn’t working and you haven’t shared your template to show some context about where this issue is happening. Maybe you’re not actually displaying post data and that’s why this isn’t working? It’s unclear. If you share your template it’ll make it easier to troubleshoot.

That being said, there’s now an (as of yet undocumented) option on the Format tag that allows you to trim another field to a certain number of words. This would be a “manual” way of creating an automatic excerpt based on a post’s content field.

<Format words=25><Field content /></Format><a href="{Field url}">[...]</a>

Hey, you’re genious ! This undocumented option work well ! Many thanks !!!

For information, here is the code which did not give me the expected result :

Configuration : Blank block thème generate by Create Block theme. (or maybe i miss something about the autocomplete exerpt configuration when a creating the theme ?!)

I have 1 template id=218 for my template card :

    <a href="{Field url}" class="b4-url-link-only">
    <div class = "b4-card">
      <If field=image exists >
        <img src="{Field image_url}" srcset="{Field image_srcset}" alt="{Field image_alt}" class="b4-image-card">
      <Else />
        <img src="http://localhost/b4dev2/wp-content/uploads/2023/09/square-background-straight.png" class="b4-image-card">
      </If>
      
        <div class="b4-card-content">
          <h2 class="b4-title-card has-large-font-size">
            <Field title />, <Field localisation_pays />
          </h2>
          <h3 class="b4-categorie-card has-medium-font-size">
            <Loop taxonomy=category post=current >
              <Field title />
            </Loop>
          </h3>
          <p class="b4-excerpt-card has-small-font-size">
           <p><Field excerpt auto=true words=10 more="..." /></p>
          </p>
        <a href="{Field url}" class="b4-card-button has-small-font-size">  
          En savoir plus <span class="dashicons dashicons-arrow-right-alt2"></span>
        </a>
    </div>
      
</div>
      </a>

And this card is calling on the loop from another template :

<div class="b4-cards-container">
  <If loop exists type=post orderby=date order=desc taxonomy=category terms="{Route part=2}" >
    <Loop>
      <Template id=218 />
    </Loop>
   
    <Else if route part="-1" is value="references"/> 
      <Loop post_type=post >
        <Template id=218 />
     </Loop>
    
    <Else />
      <p class="b4-center"> Pas de référence disponible</p>
  </If>

</div>

I also tried in only one template, but it’s doen’t neither !?

And the output result gives me the complete excerpt (with more than 10 words) if the fields are filled and empty if the field is not filled.

I remain at your disposal if you need more information and I mark this conversation as solved.

Many many thanks !