I have some pages that have a flexible layout field.
on each page, alongside the flexible layout field, there are other, different custom fields.
I want to “fire” the fields inside the flexible layout field, not all at once (inside the loop) but on demand to build the page layout.
on the backend I have:
on the frontend they should fire
What do you think is the best way to accomplish this?
Somewhere before the loop you can do the following
$queried_object = get_queried_object(); $post_id = $queried_object->ID; // get all the values for your flexible content field using the id above // and store the values in variables to use in conditional statements // example: get_field('some_field_name', $post_id);
That will be like using the
get_field_object('some_field_name') function on the page, meaning it will retrieve the entire field object – with all the subfields.
I’am looking for something more simple (if possiable). Something like
the_sub_field('sub_field') – but that can only work inside the loop.
if only there was
get_some_subfeild(field-name, subfield-name, subfiled_index/id')..
is there a way to get a subfield directly using his ID or something?
There really isn’t going to be a simple way do to this without looping through the fields and getting the values. Even going directly to the postmeta table you’ll need to loop through the fields to find the one you’re looking for and doing a loop using get_post_meta will be a lot more complicated than any other way of doing it.
I’ve build some page layout stuff using ACF flex fields. Honestly, I don’t think there is an “easy” way when you need the information that’s stored in the layout fields to determine html, classes or css values before you can display anything. There are really 2 choices 1) loop through the fields twice, once do determine layout and once for the data 2) loop thought the fields once and store the data to use as your building the layout. Of the 2 I would do the second only because it means not repeating myself.
You must be logged in to reply to this topic.
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!
© 2022 Advanced Custom Fields.