Support

Account

Home Forums Add-ons Repeater Field Display/limit one repeater image field with other sub fields

Solved

Display/limit one repeater image field with other sub fields

  • I have a repeater field with image, headline, and subtitle subfields. I’m trying to randomly show ONE row with the associated image/headline/subtitle. I used the suggested code on the Repeater Plugin page to randomize the rows and got that working. I just can’t figure out how to only display one random row instead of all of them. Is this something I do in ACF, PHP or WordPress? Anyone know?

    Alternatively, if it’s easier to display a random repeater image and its associated title/caption/description data, I could do that instead of pulling in other fields. Not sure how to do this with an image array vs image ID though. Here’s my code:

    <?php if(have_rows('home_intro')): ?>				
    <?php while(have_rows('home_intro')): the_row(); ?>	 					
    					
    	<?php 					
    	$rows = get_field('home_intro' ); // get all the rows
    	$rand_row = $rows[ array_rand( $rows ) ]; // get a random row
    					
    	$rand_row_image = $rand_row['home_intro_image' ]; // get the image sub field value 
    	$rand_row_headline = $rand_row['home_intro_headline' ]; // get the headline sub field value 
    	$rand_row_subtitle = $rand_row['home_intro_subtitle' ]; // get the subtitle sub field value 				
    											
    	$image = wp_get_attachment_image_src( $rand_row_image, 'full' );					
    	// url = $image[0];
    	// width = $image[1];
    	// height = $image[2];
    	?>
    					
    	<img src="<?php echo $image[0]; ?>" width="<?php echo $image[1]; ?>" height="<?php echo $image[2]; ?>" />									
    	<?php echo $rand_row_headline; ?>
    	<?php echo $rand_row_subtitle; ?>	
    						
    <?php endwhile; ?>							 
    <?php endif; ?>
  • Can anyone help with this please?

  • Ah, turns out I just need to remove the If and While statements. Nevermind!

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

You must be logged in to reply to this topic.