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 @qhuit

    That’s because you were still looping through the repeater field. If you want to show only one row, then you don’t need to loop through the repeater data. It should be something like this:

    <?php
    $repeater = get_field('products');
    $post_object = $repeater[array_rand($repeater)]['product_image'];
    
    if($post_object):
    
    	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'>";
    
        $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 wp_reset_postdata();
    
        echo "</div>
            </section>";
    
    endif; ?>

    Hope this helps 🙂