Fungerar utmärkt att köra som include på de sidor som behöver stöd för flexible fields
<?php
/*
* Flexible content, ACF fields on posts/pages
*
* @package WordPress
* @subpackage xxx
*/
$counters = array();
function flexible_content() {
// Check if the flexible content field has rows of data
if( have_rows('flexible_fields') ):
$field_count = 0;
// Loop through the rows of data
while ( have_rows('flexible_fields') ) : the_row();
// Update layout counter
$layout = get_row_layout();
if( !isset( $counters[ $layout ] ) ) {
// Initialize counter
$counters[ $layout ] = 1;
}
else {
// Increase existing counter
$counters[ $layout ]++;
}
$layout_class = $layout . '-' . $counters[ $layout ];
if ( get_row_layout() == 'first_layout' ):
/*
* 1st field
* ------------------------------------------------------------------
*
*/
// Outputs layout name and number
echo $layout_class;
// Outputs total count of layouts
echo 'section-' . $field_count;
$subfield = get_sub_field('subfield_name');
// Example of repeater field inside flexible fields
// check if the repeater field has rows of data
if( have_rows('grid_block_repeater') ):
// loop through the rows of data
while ( have_rows('grid_block_repeater') ) : the_row();
// Output
endwhile;
else :
// no rows found
endif;
elseif( get_row_layout() == 'second_layout' ) :
/*
* 2nd field
* ------------------------------------------------------------------
*
*/
endif;
$field_count++;
endwhile;
else :
// no layouts found
endif;
}