Support

Account

Home Forums Add-ons Options Page Displaying random values in options + repeater

Solving

Displaying random values in options + repeater

  • Hi

    First of all, I love the ACF plugins 🙂

    I’ve created an options page in order to administer a custom banner I’ve created (title, image, link).

    Now, I would like to have several banners rotating (randomly) instead of one. I’ve converted this field to a repeater field and I’ve added the other banners and it works as expected.

    However, all banners are being displayed and I would only like to display one banner only (and show another random banner if the page is refreshed).

    Is there a way to do this inside and options page + repeater field?

    Thanks!

  • I’ve created an example Field Group with the same field names and the example won’t work. In my case, its a field group that’s a repeater and is being showed in the “options” page.

    <?php

    $rows = get_field(‘repeater_field_name’, ‘option’ ); // get all the rows
    $rand_row = $rows[ array_rand( $rows ) ]; // get the first row
    $rand_row_image = $rand_row[‘sub_field_name’ ]; // get the sub field value

    $image = wp_get_attachment_image_src( $rand_row_image, ‘full’ );

    ?>
    ” />

    Tried this, no luck.

  • Hi @johanngonzalez

    The above code does not ouput anything to HTML.
    Please echo any PHP value to HTML.
    <img src="<?php echo $image; ?>" />

    Thanks
    E

  • I know this is 4-5yrs old …
    I’m having similar issues. I have the typical repeater (title, image, link) that I would like to randomize. All of my images are showing up all at once. Could someone please help me roll this row randomization into my existing code below?

    <?php if( have_rows('billboard') ): ?>
    
    	<?php while( have_rows('billboard') ): the_row(); 
    
    		// vars
    		$image = get_sub_field('image');
    		$link = get_sub_field('link');
    
    		?>
    
    			<?php if( $link ): ?>
    				<a href="<?php echo $link; ?>">
    			<?php endif; ?>
    
    				<img class="billboard" src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
    
    			<?php if( $link ): ?>
    				</a>
    			<?php endif; ?>
    
    	<?php endwhile; ?>
    
    <?php endif; ?>
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Displaying random values in options + repeater’ is closed to new replies.