Skip Navigation

[Resolved] Custom Post Types with Trim, some in the loop randomly do not show up

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.

Tagged: 

This topic contains 5 replies, has 3 voices.

Last updated by miguelG-5 6 years, 5 months ago.

Assigned support staff: Luo Yang.

Author
Posts
#294143

I am trying to:

Trim a custom post type. Display an excerpt.

I visited this URL:
hidden link

I expected to see:
All of the stories have copy above the read more link.

Instead, I got:
hidden link

Two of the posts are blank.

I was using this support page:
https://toolset.com/forums/topic/trim-multiline-custom-field/
also tried this:
https://toolset.com/forums/topic/truncate-types-field-in-views/

#294214

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Could you post/paste the PHP codes you are using here?Take a screenshot for how do you setup the custom field, I need test and debug in my localhost, thanks

#294341

Ah, of course:

<div class="news-archive-single">
<h5 style="text-transform: capitalize;">[wpv-post-link]</h5>
<div class="entry-meta"><span style="text-transform:uppercase; margin-bottom:20px">[types field="news-source"][/types]</span> | <span style="text-transform:uppercase;">[types field="original-author"][/types]</span></div>
<div class="story-image">[types field="news-story-image" size="large" align="none" resize="crop"][/types]</div>
<div class="story-excerpt">[wpv-trim-long content="[types field="news-copy" output="raw"][/types]"]
</div>
<a class="button" style="margin-top:20px;" href="[wpv-post-slug]">Read More</a>
</div>

With the excerpt line being:

<div class="story-excerpt">[wpv-trim-long content="[types field="news-copy" output="raw"][/types]"]
</div>

And the php being:

/*trim custom field */
add_shortcode("wpv-trim-long", "trim_long");
function trim_long($atts) {
    // content to trim
    $content = $atts["content"];
     
    // trim length - in words
    $words = $atts["words"];
     
    if(empty($words) || $words <= 0) {
        $words = 50;    // Default trim length
    }
     
    if(!empty($content)) {
        return wp_trim_words( $content, $words, null );
    }
}

#294482

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

I suggest you try this:
1) modify the PHP codes in your functions.php as below:

/*trim custom field */
add_shortcode("wpv-trim-long", "trim_long");
function trim_long($atts, $content) {
    extract(  shortcode_atts( array(
		'words' => 50,
	), $atts ));

    $content = do_shortcode($content);
    return wp_trim_words( $content, $words, null );
}

2) modify the excerpt line as below:

<div class="story-excerpt">[wpv-trim-long words="50"][types field="news-copy" output="raw"][/types][/wpv-trim-long]
</div>

and test again, and the new shortcode [wpv-trim-long] can be used to trim any string and shortcode.

#294721

This is perfect - and shorter simpler code.

#373041

Nice, it worked for me too