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 🙂
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🚀 This week’s session of ACF Chat Fridays dips into the preliminary results of our first ever user survey. Don’t miss it! https://t.co/3UtvQbDwNm pic.twitter.com/kMwhaJTkZc
— Advanced Custom Fields (@wp_acf) May 9, 2023
© 2023 Advanced Custom Fields.
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.