Skip Navigation

[Resolved] Trim multiline custom field

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.

This topic contains 6 replies, has 3 voices.

Last updated by Marisa 6 years, 8 months ago.

Assigned support staff: Waqas.

Author
Posts
#277062

I am trying to limit the word count of a multiline custom field (package-description).
I visited many forum topics but i still don't get it.
I'll need a shortcode to do the job and anything I tried from support forum give me an empty output.
The [wpv-post-excerpt] can't be a solution this time, but i would love to have the same effect on my field.

Thank you for your help.

#277247

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

view-settings-register-shortcode.png

Please consider following code:

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 = 30; 	// Default trim length
	}
	
	if(!empty($content)) {
		return wp_trim_words( $content, $words, null );
	}
}

Add this code in your theme's functions.php at the end. And you can use it as below:

[wpv-trim-long content="[types field='package-description' output='raw'][/types]" words="50"]

Also please remember to register [wpv-trim-long] under Views -> Settings -> "Third-party shortcode arguments" area (as shown in sample screen shot).

Please let me know if I can help you with anything related.

#277271

This code is perfect!
I just wonder if we can add a third parameter when there is the trim.
I would love to add a link to a pdf file (custom field) at the end of the content on the three little dots.

Just so far from what I know in php ...
Thank you again Waqas

    $link = $atts["link"];
#277367

I was thinking about it, maybe there is a way to set a variable to record when trim like and with this variable get a conditional like

if $content => 30 words
record a $link variable (1 or 0)
then if link = 1
display those characters [...] + href determine by custom field.

Sounds ok ?

#277457

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Yes you can use the 3rd parameter of wp_trim_words() for this purpose. The third parameter is automatically appended if the content was trimmed. In case the content was less then the trimmed length, then the content isn't trimmed, as well as, the link isn't shown.

Please consider following code (same as above), modified to cater the link:

add_shortcode("wpv-trim-long", "trim_long");
function trim_long($atts) {
	// content to trim
	$content = $atts["content"];
	
	// trim length - in words
	$words = $atts["words"];
	
	// link to place if more...
	$link = $atts["link"];
	
	if(empty($words) || $words <= 0) {
		$words = 30; 	// Default trim length
	}
	
	if(!empty($content)) {
		return wp_trim_words( $content, $words, '<a href="'. $link .'"> ...Download PDF</a>' );
	}
}

You can use the short code as:

[wpv-trim-long content="[types field='package-description' output='raw'][/types]" words="50" link="[types field='package-link' output='raw'][/types]"]

where 'package-link' is the custom field having the URL to the desired.

#278063

Exactly what was needed. And the way you write the code help me to learn.
So a huge thank you! 😉

#345951

What about trimming characters instead? Can I replace the word "words" with the word "character" in the function and shortcode? I tried this and it doesn't seem to be working.