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 ): ?>
    <?php foreach( $posts as $post): 
    setup_postdata($post); ?>
    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php endforeach; ?>
    <?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 ;).



    Edit: cleaned the code for it to display properly.