Skip Navigation

[Resolved] Choose random image from custom field

This thread is resolved. Here is a description of the problem and solution.

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 support ticket is created 7 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
- 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)

This topic contains 3 replies, has 2 voices.

Last updated by Kacey 7 years ago.

Assisted by: Noman.

Author
Posts
#584658

I have a custom, repeatable image field built called 'banner images', assigned to pages. I'd like to use a random image from that page's selection in this field in my content template– not a slideshow, but a fresh selection of the uploads to that field on each refresh. Is it possible to query the field and select one random entry for use in a content template?

#584750

Noman
Supporter

Languages: English (English )

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

register.png

Hi Allison,

Thank you for contacting Toolset support. You can achieve this using custom shortcode.

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 attached for example.

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 field name

Thank you

#584841
Screen Shot 2017-11-01 at 8.38.09 AM.png
Screen Shot 2017-11-01 at 8.38.20 AM.png
Screen Shot 2017-11-01 at 8.38.42 AM.png

Thanks! I followed these steps and it's loading the first image from the field but doesn't select randomly. Can you take a look? Here's the link:
hidden link

Screen shots attached of the steps you instructed.

#584845

Nevermind, I realized I had only changed "slider-images" in one place instead of both! Thanks for this quick solution!