Support

Account

Home Forums Add-ons Flexible Content Field Get content of layout as an array Reply To: Get content of layout as an array

  • No, there isn’t a better way if what you want is have an array with the data. Or maybe there isn’t, I might do it differently in order to remove an extensive if/elseif/elseif

    based on the code you provided

    
    // get the entire flex field
    $layouts = get_field('page_layout');
    if ($layouts) {
      foreach ($layouts as $layout_data) {
        $layout_name = str_replace('_', '-', $layout_data['acf_fc_layout']);
        $template = locate_template('component-templates/'.$layout_name.'-block.php', false, false);
        if ($template) {
          // why am I doing this?
          // if locate_template returns false, include(false) will throw an error
          include($template);
        }
      } // end foreach layout
    } // end if layouts