Skip Navigation

[Resolved] Limiting character output in Types field shortcode

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

Problem: I would like to use a Types field shortcode to display the value of a custom field, but I would like to limit the number of characters shown.

Solution: If the field holds HTML (like a WYSIWYG field, for example) then this is not a trivial task, and it would require a significant amount of custom code. However, if the field only holds plain text (like a single line or multi line text field), then you can add the following code to your child theme's functions.php file:

function trim_string_func($atts, $content='') {
  $a = shortcode_atts( array(
      'start' => 0,
      'length' => 0,
      'string' => ''
  ), $atts );
 
  $string = $a['string'];
  $start = $a['start'];
  $length = $a['length'];
  return substr($string, $start, $length);
}

Then use it like this:

[trim_string string="[types field='coach-holiday-description'][/types]" start="0" length="4"][/trim_string]
This support ticket is created 6 years 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 1 reply, has 2 voices.

Last updated by Christian Cox 6 years ago.

Assisted by: Christian Cox.

Author
Posts
#680637

I am setting up views to show results on the front end, so I have a shot description box that has any amount of text, this will appear on the full page, however on the listing page I want to reduce this box to a specific amount of characters that are shown..this is the code added, but I do not have an option to limit characters.

[types field='coach-holiday-description'][/types]

#681074

Hi, if the field holds HTML (like a WYSIWYG field, for example) then this is not a trivial task, and it would require a significant amount of custom code. However, if the field only holds plain text (like a single line or multi line text field), then you can add the following code to your child theme's functions.php file:

function trim_string_func($atts, $content='') {
  $a = shortcode_atts( array(
      'start' => 0,
      'length' => 0,
      'string' => ''
  ), $atts );

  $string = $a['string'];
  $start = $a['start'];
  $length = $a['length'];
  return substr($string, $start, $length);
}

Then use it like this:

[trim_string string="[types field='coach-holiday-description'][/types]" start="0" length="4"][/trim_string]
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.