Skip Navigation

[Resolved] Limit character output on multiline custom post type field

This support ticket is created 4 years, 11 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.

Our next available supporter will start replying to tickets in about 6.08 hours from now. 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)

This topic contains 1 reply, has 2 voices.

Last updated by Christian Cox 4 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#1418593
Screen Shot 2019-12-20 at 11.58.02 AM.JPG

While looking at the shortcode guide (see screenshot) I saw that you can limit the character output of the EXCERPT by doing something like this:

[wpv-post-excerpt length="30" count="word"]

but that same logic does NOT work with a multi line custom field?

Q1: Why not?
Q2: And how can I make that happen with a multiline field?

#1420799

Q1: Why not?
I would assume it's because excerpt is a built-in WordPress feature. Themes and plugins use the excerpt in special ways. They willv apply their own rules for trimming and formatting the main post content to create the default excerpt.
https://wordpress.org/support/article/excerpt/
https://developer.wordpress.org/reference/functions/the_excerpt/
You can also create manual excerpts, not generated by the actual post content.

The multiline custom field isn't a WordPress concept, it's just one format in which Toolset can store custom field data. WordPress doesn't have built-in filters for this value. You can use a custom shortcode to produce a trimmed version of the field content if you'd like. Add this to your child theme's functions.php file, or to a new custom code snippet in Toolset > Settings > Custom Code:

function custom_trim_field( $atts, $content = null ) {
    // Extract any arguments passed
    extract( shortcode_atts( array(
        'length'    => 55, // Number of words to show
        'more'      => ' ...',  // Text to show at the end of the filtered text
        'field'     => ''
    ), $atts ) );
    global $post;
    if ( empty($field) ){
        return;
    }
    if ( !isset($post) ){
        return;
    }
    $field = types_render_field($field, array("raw"=>"true"));
     
    $excerpt_more = apply_filters('excerpt_more', $more); // Create excerpt length to excerpt_more filter
    $output = wp_trim_words( $field, $length, $excerpt_more );  // create the filter text
    return $output;  // return filter text
}
add_shortcode( 'trimfield', 'custom_trim_field' ); 

Then you can use the shortcode like this:

[trimfield length="50" more="..." field="FIELDNICENAME"][/trimfield]

Replace 50 with the max word count, replace ... with whatever text you want to display at the end of any truncated value, and replace FIELDNICENAME with the slug of your custom field from wp-admin.