Problem:
Choose random image from custom field to use in a content template.
Solution:
1. Please add following shortcode in your functions.php file:
// shortcode to count number of repeating field instances and generate random number add_shortcode( 'generate-rand-num', 'generate_rand_num_func' ); function generate_rand_num_func($atts) { $tot_instance = sizeof(get_post_meta( get_the_ID(), 'wpcf-' . $atts['field'], false )) - 1; return rand(0, $tot_instance); }
2. Register shortcode & function first ‘generate-rand-num’ in Toolset >> Settings >> Front-end Content >> Third-party shortcode arguments. And also in >> Functions inside conditional evaluations section. Screenshot here: https://d7j863fr5jhrr.cloudfront.net/wp-content/uploads/2017/11/584750-register.png?x99441
3. And use following shortcode in Content Template:
[types field='slider-images' index="[generate-rand-num field='slider-images']"][/types]
>> Replace ‘slider-images’ with your repeating image field name.
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 |
---|---|---|---|---|---|---|
- | 12:00 – 17:00 | 12:00 – 17:00 | 12:00 – 17:00 | 12:00 – 17:00 | 12:00 – 17:00 | - |
- | 18:00 – 21:00 | 18:00 – 21:00 | 18:00 – 21:00 | 18:00 – 21:00 | 18:00 – 21:00 | - |
Supporter timezone: Asia/Karachi (GMT+05:00)