Home › Forums › Add-ons › Flexible Content Field › Nested Flexible Content Loops
I’m trying to create a jump menu (menu that uses anchor links to scroll down to areas of the page) using flexible content fields based on other flexible content fields.
| Area 1 Area 2 Area 3
* Area 1
* Area 2
* Area 3
Each area is it’s own flexible content field, and the jump menu is a flexible content field. The logic would be that the jump menu flexible content field would loop through the the other flexible content fields on the page to pull the link information.
<?php $jumpused = 0; ?> <!-- Sets count to check to see if jump menu has been used to prevent infinite loop --> <?php while( have_posts() ): the_post(); ?> <!-- check if the flexible content field has rows of data --> <?php if( have_rows('flexible_content') ): ?> <!-- loop through the rows of data --> <?php while ( have_rows('flexible_content') ) : the_row(); ?> <?php if( get_row_layout() == 'jump_menu'): ?> <?php if( $jumpused == 0 ): ?> <?php while ( have_rows('flexible_content') ) : the_row(); ?> <?php if( get_row_layout() == 'areas'): ?> <!-- Jump menu code -- > <?php endif; ?> <?php endwhile; ?> <?php endif; ?> <?php $jumpused = 1; ?> <?php endif; ?> <?php wp_reset_query(); ?> <?php endwhile; ?> <?php else: ?> <!-- No content to display --> <?php endif; ?> <?php endwhile;?>
So I have a nested loop to loop through the flexible content fields looking for the areas I need, and when the areas are found, the code to display the jump menu link for that area is fired.
The problem is, when the code returns to the main loop, the loop starts from the beginning again instead of starting after the jump menu is generated. The $jumpused variable was added to prevent an infinite loop because of this, but I can’t figure out how to prevent the loop from starting all over again.
I’m afraid I don’t quite understand your situation. Could you please share the JSON or XML export of your field group and an example of the result you want?
You must be logged in to reply to this topic.
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!
We’re hard at work on ACF 6.1, and Beta 1 is now available 🚀— Advanced Custom Fields (@wp_acf) March 16, 2023
This release includes custom post type and taxonomy registration, an improved experience when selecting field types, PHP 8.1 and 8.2 compatibility, and more!
Let’s take a look 🧵https://t.co/Y0WcAT11l4
© 2023 Advanced Custom Fields.