Skip Navigation

[Resolved] Truncate text fields

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:

0% of people find this useful.

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 topic contains 6 replies, has 2 voices.

Last updated by Beda 1 year, 3 months ago.

Assigned support staff: Beda.


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]:

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.


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

For the time being, you can build your own solution for this, by using the example and adapting it to your use-case:

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)


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:

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]

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.


Jim P.


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.


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 length="5"]
  [types field='single-field-slug'][/types]

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.


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


OK great, closing here