Home Forums Add-ons Repeater Field A repeater for 'related posts' in flex content field


A repeater for 'related posts' in flex content field

  • Hi there,

    I know it has partly been covered, and I tried both the version from the tutorials and external solutions that supposively do what I want. But I didn’t get it to work.

    It’s quite simple: Inside a flexible content field I want a repeater field to give the user the option to choose, via a select, up to four (hence the repeater) related posts from a certain/the same category, so that underneath the post itself up to four related articles display nicely with their heading and post_thumbnail. Can somebody hint me :)?

    Thanks in advance


  • Hi @physalis

    Can you please attach a screenshot to demonstrate what the data input looks like when editing the page/post?

    Your task can be broken into smaller milestones:

    1. Loop through the flexible content rows of data (see docs for examples)
    2. Then, loop through the nested repeater field (see docs for examples)
    3. For each row of the nested repeater field, load the selected post via get_sub_field function.
    4. Use the returned data (either a post, or ID – depending on your settings) to then display the title, link and thumbnail for that post

    Hope that helps.

    Please be sure to post your code if you want me to help you with it.


  • 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.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘A repeater for 'related posts' in flex content field’ is closed to new replies.