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
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
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
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
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
My issue is resolved now. Thank you so much!