Skip Navigation

[Resolved] I would like to show an excerpt of a WYSIWYG 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: I have created a WYSIWYG custom field and I would like to display only the first 100 words of that field, like an excerpt.

Solution: There is not a built-in way to truncate a WYSIWYG field like this, but you can use a custom shortcode. Add this code to your child theme's functions.php file:

function custom_trim_field( $atts, $content = null ) {
    // Extract any arguments passed
    extract( shortcode_atts( array(
        'length'    => 40, // Number of words to show
        'more'      => 'Read More...',  // Text to show at the end of the filtered text
        'field'     => '',
        'postid'    => '0'
    ), $atts ) );
    global $post;
    if ( empty($field) ){
        return;
    }
    if ( !isset($post) ){
        return;
    }
    $field = types_render_field($field, array("raw"=>"true"));
 
    $excerpt_more_link = '&nbsp;<a href="' . get_the_permalink($postid) . '">' . $more . '</a>';
    $output = wp_trim_words( $field, $length, $excerpt_more_link );  // create the filter text
    return $output;  // return filter text
 
    //example: [trimfield length="100" more="Read more..." field="wysiwyg" postid="[wpv-post-id]"][/trimfield]
}
add_shortcode( 'trimfield', 'custom_trim_field' );

Then you can place the shortcode where you want to show the truncated WYSIWYG field:

[trimfield length="100" more="Read more..." field="wysiwyg" postid="[wpv-post-id]"][/trimfield]

Replace 'wysiwyg' with the slug of your WYSIWYG custom field.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/functions/#wysiwyg

This support ticket is created 6 years, 10 months 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
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 2 replies, has 2 voices.

Last updated by nirmal-ramanK 6 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#608964

Tell us what you are trying to do?
I created a custom user field called Biography as a multiline WYSWIG entry and I would like to display this as a excerpt in a page. So I am creating a view and I would like the Biography user field to appear as EXCERPT.

Is there any documentation that you are following?

Is there a similar example that we can see?

Minimal Code Example for the view:

<td class=people align="left">
<font size=4>[wpv-user field="user_firstname"] [wpv-user field="user_lastname"]</font>, [types usermeta='role' user_is_author='true'][/types]<br/>
Email: [wpv-user field="user_email"]<br/>
Telephone: [types usermeta='telephone-number' format='FIELD_VALUE' user_is_author='true'][/types]
<!---- this generates the full biography---!>
<p> [types usermeta='biography' ][/types]</p>

<!---- Instead I would like an excerpt for 100 words with read more link pointing to a single user view---!>

</td>
</tr>

What is the link to your site?
The site is an intranet and therefore, I am not able to provide a link.

#609430

Hi, there is not a built-in way to truncate a WYSIWYG field like this, but you can use a custom shortcode. Add this code to your child theme's functions.php file:

function custom_trim_field( $atts, $content = null ) {
    // Extract any arguments passed
    extract( shortcode_atts( array(
        'length'    => 40, // Number of words to show
        'more'      => 'Read More...',  // Text to show at the end of the filtered text
        'field'     => '',
        'postid'    => '0'
    ), $atts ) );
    global $post;
    if ( empty($field) ){
        return;
    }
    if ( !isset($post) ){
        return;
    }
    $field = types_render_field($field, array("raw"=>"true"));

    $excerpt_more_link = '&nbsp;<a href="' . get_the_permalink($postid) . '">' . $more . '</a>';
    $output = wp_trim_words( $field, $length, $excerpt_more_link );  // create the filter text
    return $output;  // return filter text

    //example: [trimfield length="100" more="Read more..." field="wysiwyg" postid="[wpv-post-id]"][/trimfield]
}
add_shortcode( 'trimfield', 'custom_trim_field' );

Then you can place the shortcode where you want to show the truncated WYSIWYG field:

[trimfield length="100" more="Read more..." field="wysiwyg" postid="[wpv-post-id]"][/trimfield]

Replace 'wysiwyg' with the slug of your WYSIWYG custom field.

#611678

thank you... it worked like a charm. would be nice, if we could have this as a feature in next releases for multiline custom type elements.

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