EDIT: Updated with code which outputs the array
Should be noted I have other fields which work correctly, the issue is only with the repeater sub_fields.
// Needs to be updated to be more dynamic i know
function get_flexible_content_sections($name, $option = "") {
// check if the flexible content field has rows of data
if ( have_rows( $name, $option ) ):
// loop through the rows of data
while ( have_rows( $name, $option ) ) : the_row(); ?>
<section class="section <?= get_sub_field( "class" ) ?>">
<div class="section-content clear">
<?php while ( have_rows( "section" ) ) : the_row();
if ( get_row_layout() == 'one_width_cols' ) : ?>
<div class="full-width-col cols-12">
<?php while ( have_rows( "module_1" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<?php elseif ( get_row_layout() == 'two_width_cols' ) : ?>
<div class="half-width-col col-1 cols-6">
<?php while ( have_rows( "module_1" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<div class="half-width-col col-2 cols-6 last">
<?php while ( have_rows( "module_2" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<?php elseif ( get_row_layout() == 'three_width_cols' ) : ?>
<div class="three-width-col col-1 cols-4">
<?php while ( have_rows( "module_1" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<div class="three-width-col col-2 cols-4">
<?php while ( have_rows( "module_2" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<div class="three-width-col col-3 cols-4 last">
<?php while ( have_rows( "module_3" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<?php elseif ( get_row_layout() == 'four_width_cols' ) : ?>
<div class="four-width-col col-1 cols-3">
<?php while ( have_rows( "module_1" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<div class="four-width-col col-2 cols-3">
<?php while ( have_rows( "module_2" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<div class="four-width-col col-3 cols-3">
<?php while ( have_rows( "module_3" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<div class="four-width-col col-4 cols-3 last">
<?php while ( have_rows( "module_4" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<?php elseif ( get_row_layout() == 'five_width_cols' ) : ?>
<div class="five-width-col col-1 cols-2">
<?php while ( have_rows( "module_1" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<div class="five-width-col col-2 cols-2">
<?php while ( have_rows( "module_2" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<div class="five-width-col col-3 cols-2">
<?php while ( have_rows( "module_3" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<div class="five-width-col col-4 cols-2">
<?php while ( have_rows( "module_4" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<div class="five-width-col col-5 cols-2 last">
<?php while ( have_rows( "module_5" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<?php endif;
endwhile; ?>
</div> <!--end section-content-->
</section>
<?php endwhile;
else :
// no layouts found
endif;
}
function layout_module_slider() {
ob_start();
?><div class="layout-module-slider <?= get_sub_field('class'); ?>">
<?php
$files = get_sub_field('img_slider');
// pre is a function which wraps a value with print_r
pre($files); // <-- prints the "empty" array
if(count($files) > 0 ) {
echo slick_slider_func($files);
} else {
echo "No images available";
}
?>
</div><?php
return ob_get_clean();
}
ORIGINAL:
I have a nested repeater field inside layers of flexible contents. The purpose of which is to add images which are outputted to a slider.
The image ID's are stored in the database, but when I try to output them using get_sub_field, this is what i get:
I have build the field group programatically, which works in the backend as seen below. The backend equivalent for the above output:
The entire ACF settings/program:
if ( function_exists( 'acf_add_local_field_group' ) ):
add_action("init", "tryangle_generate_sections_fields");
function tryangle_generate_sections_fields() {
$indhold_gd = array(
'key' => 'group_indhold',
'title' => 'Indhold',
'fields' => array (),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'page',
),
),
),
'position' => 'acf_after_title',
'menu_order' => 0,
);
generate_fields( "indhold224466", $indhold_gd );
}
endif;
function generate_fields($uniqid, $group_data)
{
if(empty($uniqid) || empty($group_data)) return;
// Sets a unique affix on all keys
$uiq = $uniqid;
// Sets the group_data with locations etc.
$group_data['key'] = $group_data['key'] . '_' . $uiq;
// Available modules
$modules = array(
array(
'key' => '5b2cefcfdea9da_'.$uiq,
'name' => 'text',
'label' => 'Text',
'display' => 'block',
'sub_fields' => array(
array(
'key' => 'field_5b2d13ddfb10ba_'.$uiq,
'label' => 'Text',
'name' => 'text',
'type' => 'wysiwyg',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'tabs' => 'all',
'toolbar' => 'full',
'media_upload' => 1,
'delay' => 0,
),
),
'min' => '',
'max' => '',
),
array (
'key' => '5b3e2a167d2ee_'.$uiq,
'name' => 'image_slider',
'label' => 'Slider',
'display' => 'block',
'sub_fields' => array (
array (
'key' => 'field_5b3e2a3047a45_'.$uiq,
'label' => 'Slider',
'name' => 'img_slider',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => 0,
'max' => 0,
'layout' => 'table',
'button_label' => 'Tilføj billede',
'sub_fields' => array (
array (
'key' => 'field_5b3e2a3b47a46_'.$uiq,
'label' => 'Billede',
'name' => 'img',
'type' => 'image',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '20',
'class' => '',
'id' => '',
),
'return_format' => 'array',
'preview_size' => 'thumbnail',
'library' => 'all',
'min_width' => '',
'min_height' => '',
'min_size' => '',
'max_width' => '',
'max_height' => '',
'max_size' => '',
'mime_types' => '',
),
array (
'key' => 'field_5b3e2a5647a47_'.$uiq,
'label' => 'Billedetekst',
'name' => 'img_text',
'type' => 'wysiwyg',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'tabs' => 'all',
'toolbar' => 'full',
'media_upload' => 1,
'delay' => 0,
),
),
),
),
'min' => '',
'max' => '',
),
array(
'key' => '5b2cf0bbbf1c9a_'.$uiq,
'name' => 'posts_slider',
'label' => 'Indlægsslider',
'display' => 'block',
'sub_fields' => array(
array(
'key' => 'field_5b2d13ddfb110a_'.$uiq,
'label' => 'Indlægskategori',
'name' => 'post_category',
'type' => 'taxonomy',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'taxonomy' => 'category',
'field_type' => 'select',
'allow_null' => 0,
'add_term' => 0,
'save_terms' => 0,
'load_terms' => 0,
'return_format' => 'object',
'multiple' => 0,
),
array(
'key' => 'field_5b2d13ddfb111a_'.$uiq,
'label' => 'Udseende',
'name' => 'style',
'type' => 'select',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'choices' => array(
'Slider' => 'Slider',
'Grid' => 'Grid',
),
'default_value' => array(),
'allow_null' => 0,
'multiple' => 0,
'ui' => 0,
'ajax' => 0,
'return_format' => 'value',
'placeholder' => '',
),
),
'min' => '',
'max' => '',
),
);
// Section columns
$columns = array(
array(
'key' => 'field_one_width_cols_'.$uiq,
'name' => 'one_width_cols',
'label' => 'En kolonne',
'display' => 'block',
'sub_fields' => array(
),
'min' => '',
'max' => '',
'cols' => 1,
),
array(
'key' => 'field_two_width_cols_'.$uiq,
'name' => 'two_width_cols',
'label' => 'To kolonner',
'display' => 'block',
'sub_fields' => array(),
'min' => '',
'max' => '',
'cols' => 2,
),
array(
'key' => 'field_three_width_cols_'.$uiq,
'name' => 'three_width_cols',
'label' => 'Tre kolonner',
'display' => 'block',
'sub_fields' => array(),
'min' => '',
'max' => '',
'cols' => 3,
),
array(
'key' => 'field_four_width_cols_'.$uiq,
'name' => 'four_width_cols',
'label' => 'Fire kolonner',
'display' => 'block',
'sub_fields' => array(),
'min' => '',
'max' => '',
'cols' => 4,
),
array(
'key' => 'field_five_width_cols_'.$uiq,
'name' => 'five_width_cols',
'label' => 'Fem kolonner',
'display' => 'block',
'sub_fields' => array(),
'min' => '',
'max' => '',
'cols' => 5,
)
);
/**
* Add columns to sections
*/
$loops = 1;
foreach ($columns as $key => $col)
{
// loop for the amount of columns available (5 columns)
for ($i = 1; $i <= $col['cols']; $i++) {
$width = 100 / $loops;
$columns[$key]['sub_fields'][] = array(
'key' => 'field_module_'.$i.'_'.$uiq,
'label' => 'Modul',
'name' => 'module_'.$i,
'type' => 'flexible_content',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => $width,
'class' => '',
'id' => '',
),
'min' => '',
'max' => '',
'button_label' => 'Tilføj modul',
'layouts' => $modules,
);
}
$loops++;
}
// Add field group "Indhold"
acf_add_local_field_group($group_data);
// Add sections to the "Indhold" field group
acf_add_local_field(array(
'parent' => 'group_indhold_'.$uiq,
'key' => 'field_sections_'.$uiq,
'label' => 'Sektioner',
'name' => 'sections',
'type' => 'flexible_content',
'button_label' => 'Tilføj sektion',
'layouts' => array(
array(
'key' => 'layout_section_'.$uiq,
'name' => 'section',
'label' => 'Sektion',
'display' => 'block',
'sub_fields' => array(
array(
'key' => 'field_section_'.$uiq,
'label' => 'Sektion',
'name' => 'section',
'type' => 'flexible_content',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'min' => '',
'max' => '',
'button_label' => 'Tilføj kolonner',
'layouts' => $columns,
),
array(
'key' => 'field_section_class_'.$uiq,
'label' => 'Sektion class',
'name' => 'class',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
),
),
),
));
}
Help is very much appreciated! I've been stuck with this the entire day now. Thank you!