pee2pee
pee2pee

Reputation: 3792

WordPress ACFL Display Relationship Field Within Repeater

I have WordPress and ACF. Here's the code I use to display a repeater field

<?php

// check if the repeater field has rows of data
if( have_rows('lineup') ):

    // loop through the rows of data
    while ( have_rows('lineup') ) : the_row();

        // display a sub field value
        echo '<p>';
        the_sub_field('stage');
        echo ' on ';
        the_sub_field('date');
        the_sub_field('artists');
        echo '</p>';

    endwhile;

else :

    // no rows found

endif;

?>

If I comment out the artists line (this is the relationship field), the output are several paragraphs of stages and times. If I leave it in, the HTML of the source code stops at the end of the very first date

My question is how to display relationship fields within a repeater field

Thanks

Upvotes: 0

Views: 752

Answers (1)

pee2pee
pee2pee

Reputation: 3792

<?php

// check if the repeater field has rows of data
if (have_rows('lineup')):
// loop through the rows of data
    while (have_rows('lineup')):
        the_row();

        // display a sub field value
        echo '<p>';
        the_sub_field('stage');
        echo ' on ';
        the_sub_field('date');
        echo '</p>';

        $myposts = get_sub_field('artists');

        if ($myposts):
?>
                           <ul>
                                <?php
            foreach ($myposts as $post_object):
?>

                                    <li><a href="<?php
                echo get_permalink($post_object->ID);
?>"><?php
                echo get_the_title($post_object->ID);
?></a></li>

                                <?php
            endforeach;
?>
                           </ul>
                            <?php
        endif;
    endwhile;
else:
// no rows found
endif;

?>

Upvotes: 1

Related Questions