Limit acf dynamic content field output using twig?

Hi there,

I am trying to limit the length of an ACF field output with DC…
eg. text element with {{dc:acf:post_field field=“work_writing” | u.truncate(10, ‘…’) }}

With reference to this post… (that was truncating post title, not an ACF / Text Area field)…


Charlie suggested using twig - | u.truncate(10, ‘…’)

Does not seem to be working for me.
I have twig enabled and the String Extras ON.

The ACF field is a Text Area field type as it needs to handle line breaks and a fair bit of content.

Any ideas?
eg. Is there another twig function that is needed to convert Text Area to just Text for the u.truncate then to work?

any help much appreciated.
Cheers
Simon

Hello Simon,

Thanks for writing to us.

In order to help you with your concerns, we need to check your settings. I would request please share the admin login details meanwhile, I would suggest you troubleshoot a few of the common issues before we investigate your settings. Please share your details in a secure note. Please provide the following details

  • WordPress Login URL
  • Admin-level username and password
  • Exact page URL

You can find the Secure Note button at the bottom of your posts

Thanks

secure note added

Hello Simon,

You have used the truncate statement inside the dynamic content code which is why it does not work. You should be using the Twig code:

{{ acf.post_field({"field":"work_writing"}) | u.truncate(120, '…') }}

Check out this output:

Best Regards.

Unfortunately…
a) When the contents of the field is less than the specified truncation number of characters… I get error messages

And regardless of the trucation number - it wrecks the layout output… (scroll down the page)

Please take another look.
cheers Simon

Hello Simon,

Are the contents of the field just plain text, or does it have HTML tags in it? Try conditional truncate when the field returns less than the truncation number. For example;

{{ acf.post_field({"field":"work_writing"}) | conditional_truncate(120, '…') }}

Or you detect the length of the field by this one:

{{ acf.post_field({"field":"work_writing"}) | length > 120 ? acf.post_field({"field":"work_writing"}) | u.truncate(120, '…') : acf.post_field({"field":"work_writing"}) }}

Hope this helps.

with any length of truncation - it still completely wrecks the layout when output

Hey Simon,

The only way it breaks the layout if the field contains HTML tags. Maybe stripping off the HTML tags may work.

{{ acf.post_field({"field":"work_writing"}) | strip_tags | u.truncate(120, '…') }}

Or

{{ acf.post_field({"field":"work_writing"}) | length > 120 ? acf.post_field({"field":"work_writing"})  | strip_tags | u.truncate(120, '…') : acf.post_field({"field":"work_writing"}) }}

Hope this helps.

Thanks - much appreciated.

This worked (with striptags rather than strip_tags) ie. {{ acf.post_field({“field”:“work_writing”}) | striptags | u.truncate(120, ‘…’) }} … in terms of not breaking the layout, but for output of this content / field (as is has poems/verse as the text) it is critical that the line breaks are respected.

Can you think of any other way the field can be output with a) line breaks recpected b) truncated / limited in length c) not break the layout output?

Cheers Simon

Hello Simon,

We can exempt the BR tags from being stripped. You can try something like:

{{ acf.post_field({"field":"work_writing"}) 
   | replace({'<': '&lt;', '>': '&gt;'}) 
   | replace({'&lt;br&gt;': '<br>', '&lt;br/&gt;': '<br>', '&lt;br /&gt;': '<br>'}) 
   | u.truncate(120, '…') }}

Hope this helps.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.