Skip Navigation

[Resolved] Trim a custom field value and display it

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

Problem: I would like to trim a shortcode's value and display it.

Solution: Use this custom shortcode to trim a plain-text field to a specific number of characters:

add_shortcode('trim', 'trim_shortcode');
function trim_shortcode($atts, $content = '') {
$content = do_shortcode($content);
$length = (int)$atts['length'];
if (strlen($content) > $length) {
$content = substr($content, 0, $length) . '…';
}
return $content;
}
[trim length="20"][types field="presentation-du-programme"][/types][/trim]

Be sure to remove any line breaks or spaces inside the trim shortcode.

This support ticket is created 6 years, 7 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 6 replies, has 2 voices.

Last updated by Christian Cox 6 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#910730

Hello,
I want to show only few character on my custom field, i have find this solution . :https://toolset.com/forums/topic/limit-word-count-in-view-of-a-custom-field/

But it doesn't work anymoire i think, because the new plugin i think.

When i try my site is broken.

Do you have a solution ?

Thanks a lot

#910747

What kind of content is stored in this field? If it's HTML content, then this approach will not work. It will only work for plain text content. There is not a simple way to fix this for HTML content, because HTML adds extra words and tags and special characters, and maintaining that content and its tag strucure in a truncated format is not easy.

If you need to truncate HTML content, the best way to do that is to use CSS to specify a height and width for a container, and then hide the overflow content. It will truncate based on visual height instead of number of characters or words. Here are some tutorials and code samples showing how you can accomplish this:
https://css-tricks.com/line-clampin/
https://codepen.io/martinwolf/pen/qlFdp

#910874

Hello,
No it is a text field like in my picture.
I really don"t understand.

I have add this code in my fonction.
add_shortcode('trim', 'trim_shortcode');
function trim_shortcode($atts, $content = '') {
$content = do_shortcode($content);
$length = (int)$atts['length'];
if (strlen($content) > $length) {
$content = substr($content, 0, $length) . '…';
}
return $content;
}

And this short code
[trim length="20"]
[types field="presentation-du-programme"][/types]
[/trim]

#910876

I can't see any picture, but I will be glad to take a closer look. Please provide login credentials here so I can take a look in wp-admin. Please include a URL where I can see the problem.

#910877

The view is here : hidden link

and url here : hidden link

#910881

I did not receive login credentials, can you provide them in the private reply fields?

#911508

It looks like the spacing of the trim shortcode is causing some problems. I have modified the shortcode so that it is all on one line, like this:

[trim length="20"][types field="presentation-du-programme"][/types][/trim]

Please check now and let me know if it is still not working as expected.