Support

Account

Home Forums Front-end Issues Nested Flex or Repeater in Included File

Solving

Nested Flex or Repeater in Included File

  • Here’s the basics of what I’m doing:

    while(has_sub_field("field_name")) {
    		    $path = TEMPLATEPATH . "/included_file.php";
    		    if (file_exists($path)) {
    		    	include($path);
    		    } 
    		}

    With this I can run “the_sub_field” and “get_sub_field” in the included file. However, nested repeaters do not work with the “has_sub_field” while statement method. I can use a foreach using “get_sub_field” in a variable though.

    //this works
    foreach(get_sub_field("sub_field_name")){
    //do something
    }
    
    //this doesn't
    while(has_sub_field("sub_field_name")){
    //do something
    }

    Any ideas why the while method wouldn’t work when in an included file like that?

    Edit:

    Hmmm looking through the documentation some more, I had assumed that nesting a repeater in a Flex field worked the same way as nested repeaters. Ignore the included file aspect, can you use a while has sub field within a flex field?

  • I just tested your method, and I can confirm it’s working fine;

    One thing I did notice is that you should have added a / before ‘included_file.php’ in your path variable. I added it below!

    Code within my loop:

    
    <?php while(has_sub_field('nested')): ?>
    
    				<?php $path = TEMPLATEPATH . '/included_file.php'; ?>
    				<?php if (file_exists($path)) {
    		    			include($path);
    		    	} ?>
    
    			<?php endwhile; ?>
    

    include_file.php code

    
    <?php the_sub_field('name') ?> (<strong>
    					
    	<?php while(has_sub_field('list')): ?>
    		<?php the_sub_field('list_name') ?>
    	<?php endwhile; ?>
    </strong>)
    
    
  • Are you using two repeaters? I’m trying to call a repeater from inside a Flex field. I actually do that the ‘/’ there in my actual code, I simplified the example code. Thanks!

  • @paperrobots

    Indeed i’m using nested repeaters, I will try using a flex field.

  • @paperrobots

    Works fine with repeater inside a flex field as well. Make sure you are running this code within the WP loop or a custom loop.

    code within loop:

    <?php while(has_sub_field('nested')): ?>
    <?php $path = TEMPLATEPATH . '/included_file.php'; ?>
    	<?php if (file_exists($path)) {
    		include($path);
    	} ?>
    <?php endwhile; ?>

    include_file.php

    
    <?php if(get_row_layout() == "heading"): //another field ?>
    
    	<?php the_sub_field('heading') ?><br>
    
    <?php elseif(get_row_layout() == "repeater"): // repeater ?>
    
    	<?php while(has_sub_field('repeater')): ?>
    		- <?php the_sub_field('name') ?><br>
    	<?php endwhile ?>
    
    <?php endif; ?>
    
  • I suppose that’s my problem, I’m not running it in a loop. I’m running it from a plugin. I will try that, thanks!

  • @mikerodriguez

    Perhaps you could take a look at my plugin directly. I tried running a loop from within the plugin but no luck. Here’s the plugin: http://wordpress.org/plugins/buckets/

    In the main plugin file “buckets.php” the function get_bucket() is where I’m calling the flex field. I call the function from my ACF custom field under “fields/buckets.php”.

    Thanks!

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

The topic ‘Nested Flex or Repeater in Included File’ is closed to new replies.