Skip Navigation

[Resolved] Limiting custom field length to display

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

Problem:
Limiting custom field length to display

Solution:
You need to add custom shortcode shared with the following reply in order to limit the custom field content to display.

You can find proposed solution, in this case, with the following reply
https://toolset.com/forums/topic/limiting-field-length-to-display/#post-923899

Relevant Documentation:

100% of people find this useful.

This support ticket is created 5 years, 9 months 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.

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

Last updated by J S 5 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#923717

J S

How do I limit the length of a field that I am displaying with a Types Shortcode?

I came across a previous post from 2013 suggesting to create a shortcode to do so for the title like this:

add_shortcode('my-title', 'my_title');
function my_title($args) {
   $title = get_the_title();
   if (isset($args['length']) && strlen($title) > $args['length']) { 
     $title = substr($title, 0, $args['length']) . '…';
   }
   return $title;
}

Then, you can use

[my-title length="20"]

to display the shortened title.

How can I apply this methodology to another field? Can you please indicate how to modify the shortcode to other types fields on custom post types?

Thanks

#923899

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Could you please try to follow the following steps in order to resolve your issue.

1)
Try to add following code to your functions.php file.

add_shortcode('show_field_with_limit', 'func_limit_field_length');
 function func_limit_field_length($atts, $content = '') {
    $content = wpv_do_shortcode($content);
    $length = (int)$atts['length'];
  
    if (strlen($content) > $length) {
        $content = substr($content, 0, $length) . '…';
    }
  
    // Strip HTML Tags
    $content = strip_tags($content);
  
    return $content;
}

2)
Try to call your shortcode as given under:

[show_field_with_limit length="140"]
[types field="your-field-name" output="raw"][/types]
[/show_field_with_limit]

Where:
- Change the length attribute value as per your need.
- Replace your original field name

#953685

J S
Screen Shot 2018-07-23 at 11.01.38 AM.png
Screen Shot 2018-07-23 at 11.02.01 AM.png

This worked perfectly, thanks. I do have a followup question though related to this.

For some reason, the text input field in a text search box is getting shortened unintentionally where I am using the default text search shortcode, not the shortcode you provide above. How can I make the text field for the default text search box the full width of the box?

See screenshots showing the view used in the header, and the header itself where the search box is displayed. I don't see any css or settings in the view that should be preventing the full placeholder text from displaying, but it is being cutoff at 20 characters.

How can I fix this?

It should have anything to do with the above shortcode right?

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.