Skip Navigation

[Resolved] Displaying field but only some of the content

This support ticket is created 5 years 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by matthewL-7 5 years ago.

Assisted by: Shane.

Author
Posts
#1376403

Hi

Is there a way to display a field on a content template/view etc but only print the last 3 characters of the contents? Or first 3 characters if it is easier.

Cheers

#1376459

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Matthew,

When you say first 3 characters you are referring to the first 3 letters of that custom fields.

So if that custom field stores "Matthew" then you would just want "Mat" correct?

Please let me know.
Thanks,
Shane

#1376461

Hi Shane

Yeah correct 🙂

#1376685

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Matthew,

You should be able to do this with the shortcode below.


// Add Shortcode
function wp_trim_text( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'text' => '',
			'length' => '',
		),
		$atts
	);

	return substr($atts['text'], 0, $atts['length']);

}
add_shortcode( 'wp_trim_text', 'wp_trim_text' );

Add it to your custom code in Toolset -> Settings -> Custom Code and then activate it.

From there you should be able to use the shortcode like this.

[wp_trim_text text='type-shortcode' length='3']

Just replace types-shortcode with the actual shortcode.

Please let me know if this helps.
Thanks,
Shane

#1379267

Hey Shane

Great thanks this works perfectly. Would it be simple to show just the end 3 instead of the start 3 characters? Or is that way more complex?

Cheers

#1380079

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Matthew,

It is possible to do. All you need to do is supply the substring params with -3.

So you can do it by this, change this line from:


    return substr($atts['text'], 0, $atts['length']);

To


    return substr($atts['text'], $atts['length']);

Then provide the length attribute as length='-3'

Please let me know if this helps.
Thanks,
Shane

#1380201

My issue is resolved now. Thank you so much!