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!

    • Elliot

    • February 13, 2014 at 2:46 pm

    Hi @johanngonzalez

    Please see this doc for random row example:
    http://www.advancedcustomfields.com/resources/field-types/repeater/#template-usage

    Thanks
    E

  • 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.

    • Elliot

    • February 14, 2014 at 9:41 am

    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)

You must be logged in to reply to this topic.

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.