Support

Account

Home Forums Add-ons Repeater Field Passing value from loop to nested loop

Solved

Passing value from loop to nested loop

  • I have a template using if/while loops to display my repeater field which contains another if/while loop for a nested repeater field. The fields are category (the post) > subcategory > product. At the bottom level (product) I trigger a modal and in the header of this modal I want the values: category_name > subcategory_name > product_name …the first one is easy because I simply use the post title (the_title) the last one is also fine 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 a value from an if/while loop a nested if/while loop?

  • Use get_sub_field() to get the value and put it into a variable and then reverence that value in your nested loop.

    
    if (have_rows('repeater')) {
      while (have_rows('repeater')) {
        the_row();
        // get a field from this row to use in nested loops
        $reference_a = get_sub_field('some_field');
        if (have_rows('nested_repeater')) {
          while (have_rows('nested_repeater')) {
            the_row();
            // get a field from this nested repeater to use later
            $reference_b = get_sub_field('some_field');
            if (have_rows('nested_repeater_x')) {
              while (have_rows('nested_repeater_x')) {
                echo $reference_a ,': ',$reference_b;
              }
            }
          }
        }
      }
    }
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.