Skip Navigation

[Resolved] limit_content shortcode problems when text contains links

This thread is resolved. Here is a description of the problem and solution.

Problem: If I use the limit_content shortcode in word mode to display information from a WYSIWYG field, the content is cut off incorrectly when the field contains a link or other HTML markup.

Solution: Use wp_trim_words to strip HTML content from the custom field, or use CSS to truncate content using types field shortcodes.

This support ticket is created 6 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by nicolaT-2 6 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#786593

Hi,
I need to print an extract from a WYSIWYG custom field using the "limit_content" shortcode and I've foud that, if the original text includes a link tag, the extract is cut before that tag, regardless of the length I've set. How can I solve this?

Thanks,
Nicola

#787413

This is not a trivial task because of how HTML text links are written in code. A link HTML tag could look like this:

<a href="<em><u>hidden link</u></em>" rel="something else with spaces" class="some class names go here " id="#id-unique" data-domain="asdf">Your link text</a>

Trimming by word count works by looking for spaces in content, but you can see that the spaces in the code above do not necessarily indicate new words. The logic for interpreting where the spaces indicate new words is pretty complex. WordPress's own wp_trim_words function strips HTML tags before trimming the content based on the number of words. So if including HTML tags in the output is important, unfortunately I do not have an easy workaround for you. If the field supports HTML text, I cannot recommend trying to limit its content this way. Instead, you can try using CSS text-overflow and ellipsis properties to truncate the text, or you can use something like wp_trim_words to strip all the tags before applying the word count.

#788283

Good, I don't need the html tags so wp_trim_words is perfect.

Thank you!

Nicola