Support

Account

Home Forums General Issues Nested repeater within flexible content

Solved

Nested repeater within flexible content

  • Below is my code in a flexible content field. everything works except where I have a nested repeater called “price” within a repeater called “menu_items”.

    How do I properly output the “price” repeater?

    <?php while(has_sub_field("menu_category")): ?>
    <?php if(get_row_layout() == 'menu_category'): ?>
    <div class="article-wrap clearfix">
    	
        	<div class="colpad">
            <ul>
            <?php if(get_sub_field("menu_items")): ?>
            <?php foreach(get_sub_field("menu_items") as $x): ?>
            <li class="dontsplit">
            <h3><?php echo $x['item_name']; ?>
            <span>
                <?php if(get_sub_field("price")): ?>
                <?php foreach(get_sub_field("price") as $p): ?>
                <?php echo $p['price_option']; ?>
                <?php endforeach; ?>
                <?php endif; ?>
            </span>
            </h3>
            <p><?php echo $x['item_description']; ?></p>
            <?php endforeach; ?>
            </li>
            <?php endif; ?>
            </ul>
            </div>
    
    </div><!-- /article-wrap -->
    <?php endif; ?>
    <?php endwhile; ?>
  • Hi @theshae

    Please remember that the get_sub_field function only works WITHIN a has_sub_field or have_rows loop.

    You are correctly using a has_sub_field loop for the flexible content field, but are not using it again for the nested repeater field.

    You need to change your inner loop to:

    
    <?php if(get_sub_field("menu_items")): ?>
    	<?php while(has_sub_field("menu_items")): ?>
    	<li class="dontsplit">
    	<h3><?php echo get_sub_field("item_name"); ?>
    	<span>
    	<?php if(get_sub_field("price")): ?>
    	<?php foreach(get_sub_field("price") as $p): ?>
    	<?php echo $p['price_option']; ?>
    	<?php endforeach; ?>
    	<?php endif; ?>
    	</span>
    	</h3>
    	<p><?php echo get_sub_field("item_description"); ?></p>
    	<?php endforeach; ?>
    	</li>
    <?php endif; ?>
    

    Hope that helps.

    Thanks
    E

  • Thanks Elliot, however, I still cant get the price repeater to display. This is what I changed it to:

    <?php while(has_sub_field("menu_category")): ?> 
    <?php if(get_row_layout() == 'menu_category'): ?>
    <div class="article-wrap clearfix">
    	<?php if (get_sub_field('category_name')){?><h2><?php the_sub_field('category_name');?></h2><?php } ?>
        <?php if (get_sub_field('category_description')){?><p><?php the_sub_field('category_description');?></p><?php } ?>
        	<div class="colpad">
            <ul>
            <?php if(get_sub_field("menu_items")): ?>
    		<?php while(has_sub_field("menu_items")): ?>
            <li class="dontsplit">
            <h3><?php echo get_sub_field("item_name"); ?>
                <span>
                    <?php if(get_sub_field("price")): ?>
                        <?php foreach(get_sub_field("price") as $p): ?> 
                        <?php echo $p['price_option']; ?>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </span>
            </h3>
            <p><?php echo get_sub_field("item_description"); ?></p>
            <?php endwhile; ?>
            </li>
            <?php endif; ?>
            </ul>
            </div>
    </div><!-- /article-wrap -->
    <?php endif; ?>
    <?php endwhile; ?>
  • Nevermind Elliot, stupid mistake on my part. Your solution did work.

    Thank you very much!!

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

The topic ‘Nested repeater within flexible content’ is closed to new replies.