Home › Forums › Bug Reports › Bug(?): flexible content & nested repeater fields › Reply To: Bug(?): flexible content & nested repeater fields
Your question actually has me a little lost.
You can use get_field() for a top level repeater field, or any top level field.
get_field() should always return top level fields, not sub fields of any repeater or flex field. Using get_field() inside a repeater loop will not return values from the rows of a repeater. get_sub_field() shouldn’t get anything outside of the current repeater loop. If you are using get_field() inside a repeater then you’re probably not seeing what you think your seeing.
Here is some code illustrating a three level nesting of repeater type fields with comments about what will be returned where.
When I use get_field()
or get_sub_field()
this also refers to their equivalent function the_field()
and the_sub_field()
.
// repeater loop
// this could be a flex field or a repeater
// both are treated the same as the loop is concerned
if (have_rows('flex_field')) {
while (have_rows('flex_field')) {
the_row();
/*
using get_field() here will not
return values from rows of the flex field
get_field() will still return values from
top level fields
you must use get_sub_field() to get values
from the flex field row
*/
// repeater in the flex field
if (have_rows('repeater')) {
while (have_rows('repeater')) {
the_row();
/* using get_field() here will still only
return top level fields not not value or
the flex field or this repeater
you must use get_sub_field()
it is impossible to get other values from
the flex field until after the end of this
loop get_sub_field() will return sub fields
of this repeater only
*/
// nested repeater
if (have_rows('nested_repeater')) {
while (have_rows('nested_repeater')) {
the_row();
/*
using get_field() here will still only
return top level fields and not values of
the flex field or the first repeater
you must use get_sub_field()
get_sub_field() will only return values
from rows of this nested repeater
*/
} // end while have_rows nested repeater
} // end if have_rows nested repeater
} // end while have_rows for repeater
} // end if have_rows repeater
} // end while have_rows for flex field
} // end if have_rows flex_field
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 use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.