Skip Navigation

[Résolu] Limiting character output in Types field shortcode

Ce fil est résolu. Voici une description du problème et la solution proposée.

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 Il y a 6 années. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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)

Marqué : ,

This topic contains 1 réponse, has 2 voix.

Last updated by Christian Cox Il y a 6 années.

Assisted by: Christian Cox.

Auteur
Publications
#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.