Skip Navigation

[Resolved] I want to trim my custom field

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.

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

Problem:
How can I trim the output of a Custom Field holding text, to a certain amount of characters?

Solution:
It will require a small Custom Code Snippet.

According to our Support Policy, we do this only in a limited way.
https://toolset.com/toolset-support-policy/

This below is the code solution for both when using types only, or Views is also active on the site.
The Function must be added to your functions.php and then you use the registered ShortCode to wrap your Text Field and define, how many characters you want to strip.
The code adds a ... (3 dots) at the end of stripped content.

TYPES ONLY functions.php:

add_shortcode('trim', 'trim_shortcode');
function trim_shortcode($atts, $content = '') {
  $content = do_shortcode($content);
  $length = (int)$atts['length'];
  if (strlen($content) > $length) {
    $content = substr($content, 0, $length) . '…';
  }
  return $content;
}

Usage:

[trim length="50"]
[types field="any-text-field-you-use"][/types]
[/trim]

WITH VIEWS functions.php:

add_shortcode('trim', 'trim_shortcode');
function trim_shortcode($atts, $content = '') {
  $content = wpv_do_shortcode($content);
  $length = (int)$atts['length'];
  if (strlen($content) > $length) {
    $content = substr($content, 0, $length) . '…';
  }
  return $content;
}

Usage:

[trim length="50"]
[types field="description"][/types]
[/trim]

Debugging and evolving this above Code example is up to you.

100% of people find this useful.

This support ticket is created 8 years ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 8 replies, has 2 voices.

Last updated by Beda 8 years ago.

Assisted by: Beda.

Author
Posts
#380879

I am trying to: trim my custom field

I visited this URL: https://toolset.com/forums/topic/limit-word-count-in-view-of-a-custom-field/

I expected to see:

Instead, I got:

#380959

That Code is to use when you use Views plugin.

Types is a free Plugin, which is intended to create Data in the backend, and we do also provide some ShortCodes for it.

But Custom Programming for the Free Types Plugin is out of scope of this Free Support forum.

In the paid Toolset Forum, we can help achieve small snippets of Custom code or using the API provided by Toolset.

Toolset does not provide a API to strip or shorten content of a Field.

But it's easy to make any ShortCode function which is planned for Views, to work with types.

Just replace wpv_do_shortcode() with do_shortcode()

So your finally code will look as this:

add_shortcode('trim', 'trim_shortcode');
function trim_shortcode($atts, $content = '') {
  $content = do_shortcode($content);
  $length = (int)$atts['length'];
  if (strlen($content) > $length) {
    $content = substr($content, 0, $length) . '…';
  }
  return $content;
}

Apply it the same way as Minesh elaborates.

Thank you

#381026

Can you please create a php code for this, its just custom field, how I can add shortcode in php file?

#381034

This PHP Code above works just fine.

Furthermore, we do not Code PHP Custom Solutions.

This is specified in our Company Support Policy:
https://toolset.com/forums/ > Support Policy

But as said, I already prepared the PHP Code for you, rich with the explanation what I changed and why.

Minesh elaborated in the Thread how to use it.

You add the PHP I provided above to your theme's functions.php (use a Child Theme for your convenience) and then apply the ShortCode as illustrated by Minesh in the older thread:

[trim length="50"]
[types field="any-text-field-you-use"][/types]
[/trim]

Thank you

#381149

I know that this shortcode will work in posting but i am not doing posting, I am working on PHP file not in post. So that's why i need php code for trim this line

<?php echo types_render_field("primary", array("argument1"=>"value1","argument2"=>"value2","argument2"=>"value2")); ?>
#381153

I got the php code, is that fine

<?php echo do_shortcode('[trim length="10"] [types field="primary"] [/trim]'); ?>

and how i can remove these dots …

#381210

This is Custom code, as I elaborated.

We do not provide Custom Code.

The part you try to remove is … hidden link
(I am sorry it gets parsed as text here, look for "& hellip" in your code)

… is a HTML equivalent for ...
hidden link

Thank you

#381330

So can you please tell me what php code will be use? I have added function code which you have given me here.

#381335

I elaborated extensively that we do not Custom Code in this forum.

In the Paid Support for the Paid Toolset Plugins we do sometimes help developing Custom Application Logics.

This is a Free Plugin and a Free Support Forum.

I elaborated above, what part you need to remove.

I can not by company policy develop your Website or Code, we are here to help and support, and it is a Free Forum, where we help and support to achieve things with the Free Types Plugin.

For your convenience and simplicity here is the copy-paste ready code to use.

Please acknowledge that I do handle against Company Policy providing you Customized ready to go Solutions.

Please copy and paste to your functions.php:

add_shortcode('trim', 'trim_shortcode');
function trim_shortcode($atts, $content = '') {
  $content = do_shortcode($content);
  $length = (int)$atts['length'];
  if (strlen($content) > $length) {
    $content = substr($content, 0, $length);
  }
  return $content;
}

Thank you

The forum ‘Types Community Support’ is closed to new topics and replies.

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