Support

Account

Home Forums General Issues Get a Flexible sub field within a loop inside a while loop

Unread

Get a Flexible sub field within a loop inside a while loop

  • So here’s my setup. Each page has a flexible content field with ‘hero-image’ as a subfield.

    One section of my flex field is going to run a loop of other pages. Within that loop I want to grab a sub field that is in the same flexible content field that is running the ‘while’ loop for the page its already on.
    Slightly confusing, but when I run the “while (have rows(“my-flex-field”)” within the loop (that’s already inside the original while statement, it errors out.

    So is there something else I should be trying?

    Here’s a breakdown of the setup:

    Page 1
    while(have_rows(“content-builder”)) <–main flexible content field
    Then inside this while loop, i run a query to get other pages:
    $otherPages = new WP_Query( $args );
    Then i run:
    while ( $otherPages->have_posts() ) : $otherPages->the_post();
    And inside this loop its loading other pages that also have the “content-
    builder” flex field, and I want to grab a sub field from “content-builder”
    that exists on all those other pages:
    $hero_bg = get_sub_field(“hero_image”);

    But when i run all that, and use another ‘while(have_rows(“content-builder)) inside that nested loop, it times out on me.

    Is there a way to grab that sub field for each page I load within the loop without having to run the full while have rows again??

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.