How to limit maximum characters with '...' after

Hi there, I would like limit the number of characters that gets output for my title field inside of my loop and I want to display “…” after.

According to your docs, this should be achievable, by using the ‘length’ attribute and the ‘more’ attribute, so for example:

Then if the title of the post in the loop is greater than 40 characters long it should cut off and add ‘…’ at the end, and if its less it should just display the full title.

However, when I try this it does not work. It seems that the ‘length’ attribute works, but the ‘more’ attribute has no effect and does not display as expected on the front end.

Is this a known bug? Or am I doing something wrong? I know that the docs aren’t always up to date, so maybe something changed?

Thanks in advance for your help,

Keith

You mention length and more attributes but I don’t think those two attribute are available on the same tag, unless I’m mistaken. The words and more attributes are specifically designed to be used on the excerpt field. Since you’re looking to limit the length of another field to 40 characters, in this case I’d use the Format tag and its length attribute.

The tricky thing in this situation is to also check whether a title has been shortened and conditionally add some content (“…” in this case) afterward only when it’s been shortened. In an ideal world, we’d need some kind of longer_than comparison on the If tag, which isn’t currently a thing.

There might be other more optimal ways to achieve this but I think this is how I’d do it.

<Set shortened_title><Format length=40><Field title /></Format><Set>

<If field=title is value="{Get shortened_title}">
  <Field title />
  <Else />
  <Get shortened_title />...
</If>

Edit: I just thought of a slightly more compact approach. Not sure if formatting things twice is more or less efficient than setting a variable, but that might be worth testing with the Timer tag. I’m not sure how this would impact any HTML added to the title, so that might be worth testing if you ever have tags in your titles.

<Format length=40><Field title /></Format>

<If field=title is_not value="{Format length=40}{Field title /}{/Format}">...</If>

@iamkeef22 it was just brought to my attention that the as-of-yet-undocumented <Format count> feature can be used to check the number of items in a list or check the number of characters in a string. I just tested it out and it seems to work and doesn’t require shortening every title or setting any variables. This is likely the most efficient approach to do what you’re trying to do at the moment.

<If check="{Format count}{Field title /}{/Format}" more_than value=40>
  <Format length=40><Field title /></Format>...
  <Else />
  <Field title />
</If>
1 Like