1
votes

I have nested repeater fields with Advanced Custom Fields, in my template I use if while loops to display them. The fields are category (the post) > subcategory > product. At the bottom level (product) I trigger a modal, in the header of this modal I want category_name > subcategory_name > product_name …the first one works fine because I simply use the post title (the_title) the last one also works because I am at that level and only have to call the_sub_field('product_name') but I don't know how to call the subcategory name value, I have tried the_sub_field('subcategory_name') and the_field('subcategory_name') both returning blank. Is there a way to pass the value from the parent loop to the child?

Here's what it looks like:

<?php if( have_rows('category') ): while ( have_rows('category') ) : the_row(); ?>
    <h3><?php the_sub_field('category_name'); ?></h3>
    <?php if( have_rows('product') ): while ( have_rows('product') ) : the_row(); ?>
        <a href="#<?php the_sub_field('modal_id'); ?>" data-toggle="modal" >
            <?php the_sub_field('product_name'); ?>
        </a>
        <!-- THE MODAL -->
        <div id="<?php the_sub_field('modal_id'); ?>" class="modal fade" ...>
        <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="myModalLabel">
                    <?php the_title(); ?>
                    &gt;
                    <?php the_sub_field('category_name'); ?><!-- DOESN'T WORK -->
                    &gt;
                    <?php the_sub_field('product_name'); ?>
                </h4>
            </div>
            <div class="modal-body">
                Product details here
            </div>
        </div>
        </div>
        </div>
    <?php endwhile; endif; ?>
<?php endwhile; endif; ?>
1

1 Answers

1
votes

If i understand correctly you want to display data from the first loop in the second loop. That can be done by storing the data first. like this:

<?php if( have_rows('category') ): while ( have_rows('category') ) : the_row(); ?>
    <h3><?php the_sub_field('category_name'); ?></h3>
    <?php $category_name = get_sub_field( 'category_name' ); ?>
    <?php if( have_rows('product') ): while ( have_rows('product') ) : the_row(); ?>
        <a href="#<?php the_sub_field('modal_id'); ?>" data-toggle="modal" >
            <?php the_sub_field('product_name'); ?>
        </a>
        <!-- THE MODAL -->
        <div id="<?php the_sub_field('modal_id'); ?>" class="modal fade" ...>
        <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="myModalLabel">
                    <?php the_title(); ?>
                    &gt;
                    <?php echo $category_name; ?><!-- NOW IT WORKS -->
                    &gt;
                    <?php the_sub_field('product_name'); ?>
                </h4>
            </div>
            <div class="modal-body">
                Product details here
            </div>
        </div>
        </div>
        </div>
    <?php endwhile; endif; ?>
<?php endwhile; 

$category_name will get a new value with every outer loop.