Skip Navigation

[Resolved] excerpt from custom field multilines – with breaks

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

Problem:
excerpt from custom field multilines - with breaks

Solution:
You need to add custom shortcode to count number of words and display the read more link accordingly.

You can find the proposed solution, in this case with the following reply:
=> https://toolset.com/forums/topic/excerpt-from-custom-field-multilines-with-breaks/#post-1255003

Relevant Documentation:
https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

This support ticket is created 5 years, 5 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 0.87 hours from now. 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)

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by Ido Angel 5 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#1254901

hey,
i used this great advice here for creating excerpts from a custom field:

https://toolset.com/forums/topic/i-would-like-to-show-a-excerpt-of-a-custom-user-field-that-i-have-created/

point is, my custom field is not WYSIWYG, but multiline textarea.

when i present it in the content template, without the excerpt shortcode, the breaks are there, e.g:

line one
line two
etc...

but when i use the excerpt shortcode, the breaks are gone, e.g:

line one line two etc...

this is the snippet:

function custom_trim_field( $atts, $content = null ) {
    // Extract any arguments passed
    extract( shortcode_atts( array(
        'length'    => 40, // Number of words to show
        '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' );

any ideas?

thx!
ido

#1254919

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - can you please share problem URL where you want to display excerpt and what is your excerpt field with which you have issues that it does not render line breaks.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#1255003

Minesh
Supporter

Languages: English (English )

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

Well - I've to change the code you added a bit.

I've moved the code you added to your theme's functions.php file to Toolset's custom code section
=> hidden link

Here is the modified code:


// Put the code of your snippet below this comment.
function custom_trim_field( $atts, $content = null ) {
    // Extract any arguments passed
    extract( shortcode_atts( array(
        'length'    => 40, // Number of words to show
        '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"));
	$field = nl2br(get_post_meta($atts['postid'],'wpcf-'.$field,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
	
 $words = preg_split("/[\n\r\t ]+/", $field, $length + 1, PREG_SPLIT_NO_EMPTY);
    if ( count($words) > $length ) {
      
        
        array_pop($words);
        $text = implode(' ', $words);
        $field = $field . $excerpt_more_link;
    } else {
        $field = implode(' ', $words);
    }
	
	 return $field;  // return 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' );

More info:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

I can see that it works as expected now. Can you please confirm.

#1255007

My issue is resolved now. Thank you minesh!!!