[Resolved] Truncate text fields

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.

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

Problem: How can I truncate the output of a Field to a specified amount of characters?

Solution: There is no inbuilt solution for that unless for the excerpt shortcode.

For other fields, you might find this custom solution helpful: https://toolset.com/forums/topic/truncate-text-field-in-blocks-editor/#post-1470587

This topic contains 6 replies, has 2 voices.

Last updated by Beda 3 weeks, 5 days ago.

Assigned support staff: Beda.

Author
Posts
#1465073

Tell us what you are trying to do?
I'm needing to truncate a long text field for use in the archive view (a custom field of a CPT).

Is there any documentation that you are following?
I've done this in past (pre-Gutenberg), possibly via custom shortcode as your support recommends. I also see this shortcode, but no not see how to specify which field [I assume it is N/A]: https://toolset.com/documentation/user-guides/views/views-shortcodes/#wpv-post-excerpt

Is there a similar example that we can see?
My older sites where I did this have been archived, unfortunately, and they were pre-Gutenberg; I can't understand with the block editor whether I can truncate as part of the block settings.

What is the link to your site?
I'm still developing it; sorry.

#1465295

Beda
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

That's right, word and character count is available only for the excerpt ShortCode of Toolset Views.

I'd suggest to eventually submit this as a feature request, to add some sort of truncating mechanism for fields where it makes sense.
You can do that here https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

For the time being, you can build your own solution for this, by using the example and adapting it to your use-case:
https://toolset.com/forums/topic/truncate-wysiwyg-content/#post-159880
https://toolset.com/forums/topic/i-want-to-trim-my-custom-field/

Now, this is still a ShortCode, where you can specify the field - you'd do that the same way as before, and insert it in Gutenberg inside a ShortCode Block or other (HTML/Classic/Text and Fields)

#1466121

Beda, this all makes sense. I had attempted a shortcode solution previously and it did not work so I assumed I had done something wrong. Following your reply, I followed exactly your previous support thread:

https://toolset.com/forums/topic/i-want-to-trim-my-custom-field/

Inserting the php via the Code Snippets plugin as I have so many times in past (after verifying via a simple shortcode that Code Snippets works fine via my new server host):

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;
}

I then inserted a simple shortcode in the archive view for this CPT (Fields and Text block, HTML pane to avoid formatting) as follows:

 
[trim length="50"]
[types field='background-references'][/types]
[/trim]

The result for this shortcode in archive view was blank. As I look at PHP errors I get the fairly unhelpful "unknown location," and am at a loss to diagnose what's wrong with the trim shortcode.

If you can see anything obvious, I'd appreciate. For some reason this particular trim shortcode does not work, though right above it in the archive view a standard wpv-user shortcode works just fine.

Regards,

Jim P.

#1466155

A quick update with two things. First, link to archive view referenced above (site is not yet public but page viewable):

hidden link

And a note upon viewing that page while logged out: the wpv-user shortcode does not execute unless logged in, i.e., is blank to guests. (I have nothing in Toolset Access that would affect this, and the other Types field shortcode displays fine.) There must be some obvious restriction on guests viewing usernames of which I'm unaware.

Thanks again,

Jim P.

#1470587

Beda
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

It works perfectly fine, you must be experiencing some other issue that breaks ShortCodes.

Please open a new ticket for the issue with the User ShortCode, it may be related but speaks for a problem with either a theme or plugin, that breaks this shortcode or more.

As for the custom shortcode, this works great, I elaborate it here again:

1. Insert this to the Theme's functions.php

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;
}

We do nothing else but register a ShortCode here that takes its content, expands it and trims it

2. Then, in a Post, or anywhere else (also archive loops) you can do this:

[trim length="5"]
  [types field='wysiwyg-field-slug' output='raw'][/types]
[/trim]
OR
[trim length="5"]
  [types field='single-field-slug'][/types]
[/trim]
ETC

This will trim those fields contents of available to 5
This shortcode is 100% independent from Toolset, you could use anything in the content.
It just trims what it gets as input.

Now, if this does not work I suggest to change the Theme (to Twenty Twenty) and disable other plugins, to check what it is that makes it stop working.
I can then help with either suggesting solutions, reaching out to the third party if needed, or else as required.

#1475905

Hmm, just re-entered your most recent shortcode in Snippets and it now works!...we can close, thanks...Jim P.

#1476285

Beda
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

OK great, closing here