Home › Forums › General Issues › Getting first row from repeater field from post object in flexible content
I know the title of my question is a little confusing, so I will first explain what I am trying to do.
I have a flexbile content field, where I have made a post object field which is multi select. So I can select multiple posts in that field.
Now the posts which are selected in that field, there is a repeater field in those posts, which has multiple data, like title, description. I want to grab the first row of each repeater field.
So if I select 2 posts in post object. It will look for those 2 posts and grab the Title, Description from the first row of the repeater field of those 2 posts.
I hope I have made myself clear ?
So here is the code I am using to display it in the frontend.
<?php
if( have_rows('customer_landing_page') ):
while ( have_rows('customer_landing_page') ) : the_row();
if( get_row_layout() == 'success_stories' ): ?>
<div class="inner">
<div class="content-wrap">
<div class="post-content">
<h2>Client Success Stories</h2>
<?php $post_objects = get_sub_field('client');
if( $post_objects ): ?>
<ul>
<?php foreach( $post_objects as $post): ?>
<?php setup_postdata($post); ?>
<?php if (get_field('statistics')) : ?>
<?php while(has_sub_field('statistics')): ?>
<li>
<div class="stats-wrap">
<p class="stats-number"><?php the_sub_field('number'); ?></p>
<div class="stats-content">
<p><?php the_sub_field('description'); ?></p>
<a href="<?php the_permalink(); ?>">Find Out More</a>
<?php the_post_thumbnail( 'thumbnail', array( 'class' => 'aligncenter' ) ); ?>
</div>
</div>
</li>
<?php break;
endwhile;
endif;
endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
</div>
</div>
<?php endif;
endwhile;
endif;
?>
So as you can see, I am first running a while loop for flexbile content, and then using if row layout for that section. Then setting up post data for the post object.
I am using the break; for the repeater field, so that the loop breaks after first run and advances to the next post.
However, the problem that I am facing is, that it is outputting only 3 posts, while there are 4 posts, and the other weird thing happening is that it is running into an infinite loop, with the flexible content field repeating itself several times.
Here is a screenshot of the output so you can understand what’s going on.
http://i57.tinypic.com/2qaux04.jpg
This is just a part of screenshot. This row keeps on repeating till php execution time is exceeded.
When I use the_title() inside the post object, then it shows those 4 posts that I have selected.
Thanks !
You must be logged in to reply to this topic.
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!
ποΈ Just one more day until the next session of ACF Chat Fridays! Make sure to register for the latest updates on whatβs coming in ACF 6.1!
— Advanced Custom Fields (@wp_acf) March 30, 2023
π Friday 31st March 3pm UTC
π Register here - https://t.co/3UtvQbDwNm pic.twitter.com/7xtEJakeQN
© 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.