Support

Account

Home Forums Add-ons Repeater Field Repeater RANDOM From Another Page

Solved

Repeater RANDOM From Another Page

  • I am familiar with the repeater function, but have not used the repeater random function and try to get from another page. Here is the code I current have tried but not getting any results:

    <?php 
    $other_page = 24;
    $rows = get_field('clapper_images', $other_page); // get all the rows
    $rand_row = $rows[ array_rand( $rows ) ]; // get the first row
    $rand_row_image = $rand_row['people_clappers_images' ]; // get the sub field value 
    $image = wp_get_attachment_image_src( $rand_row_image, 'full' );
    ?>
    <img src="<?php echo $image[0]; ?>" />

    Any help will be great.

  • Hi @RE20

    Your code looks great. Have you debugged it line by line?
    You can debug your code like so:

    
    <?php 
    
    echo '<pre>';
    	print_r( $rand_row  );
    echo '</pre>';
    die; ?>
    

    Do this for each line of code, and obviously change the $variable as you go through each line. This way you can see what data is in your variables.

    You may find that there is a field name spelling mistake, or perhaps the return value of the image is an object, not an ID?

    Thanks
    E

  • Ok, thanks. I did some more research and came across this coding:

    <?php while(the_repeater_field('clapper_images', '24')): ?>
    <?php if($rand == $i): ?>
    <?php the_sub_field('people_clappers_images', '24'); ?><?php endif; ?><?php $i++; ?>
    <?php endwhile; ?>

    It works, but only echos out one image and the same image every time. Any ideas how to get it to echo out a random image each time.

  • got it figure out

    <?php $rand = rand(0,4); $i = 0; ?>
    <?php while(the_repeater_field('clapper_images', '24')): ?>
    <?php if($rand == $i): ?>
    <?php the_sub_field('people_clappers_images', '24'); ?>
    <?php endif; ?><?php $i++; ?>
    <?php endwhile; ?>
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Repeater RANDOM From Another Page’ is closed to new replies.