Support

Account

Home Forums Add-ons Options Page Options + Random Repeater

Solved

Options + Random Repeater

  • Having some issues using the random repeater code for options field, nothing is returned for the img src.

    My code so far:

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

    I think my formatting of $rand_row_image is the issue perhaps. Any ideas?

  • Hi @denty

    If you’ve set the image field to return the attachment ID then this should work. You where almost there.

    
    <?php 	
    	$rows = get_field('hero_images', 'options' ); // get all the rows
    	$rand_row = $rows[ array_rand( $rows ) ]; // get the first row
    	$rand_row_image = $rand_row['hero_image']; // get the sub field value 
    	$image = wp_get_attachment_image_src( $rand_row_image, 'full' );
    ?>
    <img src="<?php echo $image; ?>" />
    
  • Thanks Jonathan, yes it the field is an id.

    This sorted it:

    <?php echo $image[0]; ?>

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Options + Random Repeater’ is closed to new replies.