Home › Forums › Add-ons › Repeater Field › A repeater for 'related posts' in flex content field › Reply To: A repeater for 'related posts' in flex content field
Hi elliot,
thanks for your fast reply as usual 😉 .
Your mention of ‘either a post or ID’ hinted me to the right way. What I did so far: use a repeater field and let the user add a row for each related posts with a select. It was a post objects subfield which I thought to be the right one to go. Now I swiped the repeater field and took a relation field – which is much more sexy since you can see and rearrange your posts in a list. I finally got it working this way!
My code, if anyone interested:
<?php while(the_flexible_field('factsheet')): ?>
<?php if( get_row_layout() == 'factsheet' ): ?>
<?php $posts = get_sub_field('related_posts');
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post):
setup_postdata($post); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
<?php endif; ?>
<?php endwhile; ?>
Thanks again, and good to know you’re around ;).
Best
physalis
Edit: cleaned the code for it to display properly.
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!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 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.