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 = ' <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
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 6 years, 10 months ago.
Assisted by: Christian Cox.
The forum ‘Types Community Support’ is closed to new topics and replies.