I’m trying to have Relationship fields, inside a repeater which is placed in a flexible content layout. Is that possible?
I was able to make the repeater work in the flexible content layout, but I can’t get the relationship data to work.
For the short story:
It’s a schedule for a conference. I use the flexible content to build the schedule. I already have talks, breaks and parties, and I want to have workshop sessions. I use my repeater field for the workshops and instead of filling the fields, I use the relationship to get the infos directly from the speakers pages.
Here’s my code, it obviously doesn’t work but that’s what I’d like.
(the rest of the flexible content is working fine for my other type of events (talks, breaks, parties)
<?php // START LAYOUT WORKSHOPS elseif( get_row_layout() == 'workshop_session' ):?> <?php if(get_sub_field('workshop')): ?> <?php while(has_sub_field('workshop')): ?> <?php $posts = the_sub_field('workshop_speaker'); if( $posts ): ?> <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?> <?php setup_postdata($post); ?> <p><?php the_title(); ?></p> <!-- this would be the name of the speaker I use on the speaker page that I "call" using the relationship field --> <?php endforeach; ?> <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?> <?php endwhile; ?> <?php endif; ?> <?php // END LAYOUT WORKSHOPS endif; ?>
Do you know what I’m doing wrong?
Try this, just before the_title, try to echo the $post->ID. At least then you will know if you are getting the correct post or if there is a disconnect between the two.
You can also try this:
$ID = $post->ID; echo get_the_title( $ID );
You can also try this, this is code from my page where i use
$posts = get_sub_field('carousel_project'); // relationship field if( $posts ): ?> <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?> <?php setup_postdata($post); ?> <?php if( have_rows( 'repeater_multimedia' ) ): ?> // repeater field inside of the related post <?php while( have_rows( 'repeater_multimedia' ) ): the_row(); ?>
Maybe if you change your top part to this, you might have better luck.
Actually, I think the problem is that
<?php $posts = the_sub_field('workshop_speaker'); if( $posts ): ?>
should use “get_sub_field” instead, like so:
<?php $posts = get_sub_field('workshop_speaker'); if( $posts ): ?>
I was having a similar problem, and found that solution here which worked for me.
Hope that helps!
The topic ‘Relationship < Repeater < Flexible content’ is closed to new replies.
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!