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 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 1 reply, has 2 voices.

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

Assigned support staff: 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]