Support

Account

Home Forums Add-ons Flexible Content Field Nested Flexible Content Loops

Helping

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.

    Something like:

    |
    | 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.

    The code:

    <?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.

  • Hi @joelatcomit

    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?

    Thanks!

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

The topic ‘Nested Flexible Content Loops’ is closed to new replies.