Home › Forums › Add-ons › Repeater Field › Random Image Repeater › Reply To: Random Image Repeater
Do you have caching plugin on your site or your hosting? If you have caching, then it’ll serve the static cache file directly without running through your php code. You might want to consider using js to do the randomization in that case.
Something like:
<?php
$availableImages = [];
while (have_rows('repeater_field_name')): the_row();
$availableImages[] = wp_get_attachment_image_url(get_sub_field('sub_field_name', 'full'));
endwhile;
?>
<script>
var availableImages = <?php echo json_encode($$availableImages); ?>;
jQuery(function($) {
var randomImage = availableImages[Math.floor(Math.random() * availableImages.length)];
$('.hero').css('background-image', 'url(' + randomImage + ')');
});
</script>
Cheers.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 7, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.