Support

Account

Home Forums Add-ons Flexible Content Field Get flexible layout field subfield outside loop

Solved

Get flexible layout field subfield outside loop

  • Hey

    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.

    for example:

    on the backend I have:

      flexible layout field

        field-1
        field-2
        field-3

      Other field-1
      Other field-2

    on the frontend they should fire

      flexible layout field – field-1
      Other field-1
      Other field-2
      flexible layout field – field-2
      flexible layout field – field-3

    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);
    
  • Thanks John

    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.

  • OK thank you very much!

    Just wanted to be sure that I’am doing it the hard way with no reason

  • 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.

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

The topic ‘Get flexible layout field subfield outside loop’ is closed to new replies.