Problem:
How to add a conditional test for the length of the value of a custom field.
Solution:
Register a custom shortcode to get the length of a field passed as an attribute for the current post by adding the following to your theme's functions.php:
/** * Register shortcode "fieldlength" * * requires attribute field = "slug" of field to measure length of */ add_shortcode( 'fieldlength', function( $atts ){ global $post; $string = get_post_meta( $post->ID, $atts['field'], true); return strlen( $string ); });
Register the custom shortcode at Toolset > Settings > Front-end Content and then use it inside a wpv-conditional shortcode like so:
[wpv-conditional if="( '[fieldlength field='wpcf-author-name']' gt '15' )"] Length > 15 [/wpv-conditional]
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 |
---|---|---|---|---|---|---|
- | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | - |
- | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | - |
Supporter timezone: Europe/London (GMT+00:00)
This topic contains 3 replies, has 2 voices.
Last updated by 6 years, 11 months ago.
Assisted by: Nigel.