Skip Navigation

[Resolved] Limit characters in views field

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 11 replies, has 1 voice.

Last updated by Minesh 9 months, 3 weeks ago.

Assisted by: Minesh.

Author
Posts
#2783217

Dear Support,

I would like to know if this is still the best way to limit characters within a text field that appears in the views area of a website:

https://toolset.com/forums/topic/limiting-character-output/#post-681074

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]

I would also like to add three period dots at the end of the reduced character text.

I thank you for your time and assistance.

#2783219

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Are you already using the code you shared with the reference ticket?
- https://toolset.com/forums/topic/limiting-character-output/#post-681074

If yes:

Then what if you try to update the code as given under and check if that help you to display the separator dots at the end:

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

  return substr($string, $start, $length)." ".$separator;
}
#2783220

Thank you Minesh for your support. I have not applied any code at the moment. Should I consider a different code implementation or would it be best to use what you have suggested above?

#2783221

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

To limit the number of characters for the multiline custom field, you will have to use such custom shortcode.

Yes, can you please try to use the custom shortcode, you can add the custom shortcode to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

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

And you can call it as given under:

[trim_string string="[types field='your-custom-field-slug'][/types]" start="0" length="50"][/trim_string]

Where:
- Replace the "your-custom-field-slug" with your original custom field slug.
- Replace the "length" attribute value as required, currently its set to 50.

#2783223
Captura desde 2024-11-12 12-33-31.png

Hello Minesh,

Unfortunately, I am receiving an error on line 14 of the code snippet:

syntax error, unexpected single-quoted string "string", expecting ")" in /home/callejh/www/wp-content/toolset-customizations/limit-characters.php on line 14

** Se ha actualizado en fragmento "limit-characters".
Ocurrió un error al tratar de volver a ejecutar el fragmento.syntax error, unexpected single-quoted string "string", expecting ")" in /home/callejh/www/wp-content/toolset-customizations/limit-characters.php on line 14 A problem occurred when executing snippet "limit-characters". The result of include_once is: ""

#2783228

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ahh, comma is not added.

Can you please try to use the following code:

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

Hello Minesh,

That code work fine but I am not seeing changes to the field that I am trying to trim or limit the characters.

I placed the following code:

[trim_string string="[types field='descripcion-de-la-colonia'][/types]" start="0" length="5"][/trim_string]

It is displaying all of the text and not reducing the characters to 5.

Is there something else that I need to do to make this code active or work? Thank you.

#2783230

Hello Minesh,

The code now works without errors but unfortunately after applying the code below I am not seeing any reduction of characters. The field is still displaying all of the text and I am expecting it to be reduced to 5 characters with trailing ....

[trim_string string="[types field='descripcion-de-la-colonia'][/types]" start="0" length="5"][/trim_string]

Do I need to do something to make the code active or work? Thank you.

#2783231

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

That is strange.

Can you please share details where you added the custom shortcode and send me admin access details.

I have set the next reply to private which means only you and I have access to it.

#2783234

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

To the "Custom Code" section I've adjusted the code added as given under:
=> hidden link

add_shortcode("trim_string","trim_string_func");
function trim_string_func($atts, $content='') {
  global $post;
  $a = shortcode_atts( array(
      'start' => 0,
      'length' => 0,
      'separator' => '...',
      'field' => ''
  ), $atts );
    
  $string = get_post_meta($post->ID,"wpcf-".$atts['field'],true);
   
  if(!empty($string)){
 	$start = $a['start'];
  	$length = $a['length'];
  	$separator = $a['separator'];
    return substr($string, $start, $length)." ".$separator;
  }else{
    return '';
  }
  
}

And I've added the shortcodes block and called the shortcode as given under:

[trim_string field="descripcion-de-la-colonia" start="0" length="5"]
#2783236

Thank you Minesh!

Did you have a chance to reference the video that I recorded for you showing how my styling changes are not staying permanently on the site?

Here is the link: hidden link

Thank you for your assistance.

#2783238

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

As per our support policy, we entertain only one question per ticket. This will help other users searching on the forum.

I've split the ticket with your new question and I will get in touch with you with the following split ticket:
- https://toolset.com/forums/topic/split-limit-characters-in-views-field-block-view-style-lost-on-frontend-flatsome-theme/