Tell us what you are trying to do?
I've added the custom_trim_field function as described in https://toolset.com/forums/topic/truncate-wysiwyg-custom-field/. I am calling it from within a View. It works if I set field="calendar-event-contact-name", where this field is part of my Calendar Events post type. However, I can't figure out how to do a call to truncate the display of the post_content.
This works:
[trimfield length="2" more="Carry On..." field="calendar-event-contact-name"]
These don't work:
[trimfield length="20" more="Carry On..." field="wpv-post-id"]
[trimfield length="20" more="Carry On..." field="wpv-post-body"]
Thanks in advanced for your help...
Hi,
Thank you for contacting us and I'd be happy to assist.
The code from the other ticket was designed to get the text only from the custom fields. It doesn't cover the 'post_content'.
To extend it to include the support for the 'post_content', you can update its snippet to:
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' => ''
), $atts ) );
global $post;
if ( empty($field) ){
return;
}
if ( !isset($post) ){
return;
}
if ($field == 'wpv-post-body') {
$field = apply_filters('the_content', get_the_content());
}
else
{
$field = wp_strip_all_tags(types_render_field($field, array("raw"=>"true")));
}
$excerpt_more = apply_filters('excerpt_more', $more);
$readmorelink = '<a href="'.get_permalink($post->ID).'">'.$excerpt_more.'</a>';
$output = wp_trim_words( $field, $length, $readmorelink );
return $output;
}
add_shortcode( 'trimfield', 'custom_trim_field' );
After that you'll be able to use the shortcode for the content and the fields like this:
// For post content
[trimfield length="20" more="Carry On..." field="wpv-post-body"]<hr>
// For post field 'field-slug'
[trimfield length="10" more="Carry On..." field="field-slug"]
I hope this helps and please let me know if you need further assistance.
regards,
Waqar
Thank you for your quick reply. You give world class support!
Alas, the new code targeting wpv-post-body didn't work for me. When called, the page probably goes in a loop - it never resolves. I ECHOed the get_the_content() and saw that it returns the name of the View that called the function instead of the record in the loop.
I decided it was cleaner for me to add a new field to the form/view for the portion of the body I needed, so that I could call the original version of the function. Thus, I'm abandoning the thought of trying to access/truncate the wpv-post-body.
I'll leave this item open for a couple days in case you want me to test any other suggested code updates.
Thanks again...
Glad I could help and appreciate the kind words.
Can you please test if replacing the line:
$field = apply_filters('the_content', get_the_content());
With this line, makes any difference?
$field = $post->post_content;
That change works - thanks!
I'll leave this item open for a couple days in case you want me to test any other suggested code updates, but I'm ready to close this when you are.
Thanks for the update and glad that it works.
Unless you have any follow-up questions, you're welcome to mark this ticket as resolved.
For a new question or concern, feel free to start a new ticket - we're here to help!