Support

Account

Home Forums Front-end Issues Showing one random post object from a sub_field list Reply To: Showing one random post object from a sub_field list

  • Hi @James and thanks a lot for your answer, you (partially) saved me !!

    The only problem I’ve got is that it sometime shows one product many times…Any idea ?

    <?php
    
    if( have_rows('products')):
    
    	echo "<section class='shopThis'>
    			<h3 class='title md-col-3'>Shop this in our store</h3>
    			<!--<p>This article is available in our brand new online store.<br>-->
    			<p>We brought a few things back from this trip, and it's now available in our brand new online store.<br />
    			Gets yours by clicking here!</p>
    			
    			<div class='articles_container'>";
    	while ( have_rows('products') ) : the_row();?>
    
    				<?php
    		$repeater = get_field('products');
    		$post_object = $repeater[array_rand($repeater)]['product_image'];
    
    				if( $post_object):
    					$post = $post_object;
    					setup_postdata($post);
    					?>
    
    			<article>
    				<a href="<?php echo the_permalink();?>"><?php the_post_thumbnail();?></a>
    				<a href="<?php echo the_permalink();?>" class="btn">shop this</a>
    			</article>
    
    				<?php endif;?>
    		<?php wp_reset_postdata();?>
    
    <?php 	endwhile;
    
    echo "</div>
    	</section>";
    
    endif;
    
    ?>
    
    <?php
    /*
    
    if( have_rows('products')):
    
    	while ( have_rows('products') ) : the_row();*/?><!--
    
    		<?php
    /*		$post_object = get_sub_field('product_image');
    		if( $post_object):
    
    			$i = 0;
    			foreach( $post_object as $post):
    			$post = $post_object;
    			setup_postdata($post);
    			array_rand((array)$post);
    			*/?>
    
    			<a href="<?php /*echo the_permalink();*/?>"><?php /*the_post_thumbnail();*/?></a>
    	<?php /*if (++$i == 2) break;*/?>
    
    		<?php
    /*				wp_reset_postdata();
    			endforeach;
    
    		endif;*/?>
    
    	--><?php /*	endwhile;
    
    endif;
    
    */?>
    

    Thanks again for your help 🙂