Hi,
I am using flexible content with several layouts. Is there a way to count the number of a specific layout?
For example, I have a layout called staff_member which contains several fields. I want to know how many staff_member layouts there are so I can add a class of odd or even to a div with class of “staff”. Or maybe there’s a more simple way to add a class of odd or even. CSS nth formatting doesn’t work because staff members divs are interrupted by department title divs.
Here is the page: http://jarrow.org/staff/
Thanks
/* Staff Custom Fields
------------------------------------------------------------ */
function wnd_staff() {
// check if the flexible content field has rows of data
if( have_rows('staff_content') ): ?>
<div class="staff-content">
<?php // loop through the rows of data
while ( have_rows('staff_content') ) : the_row();
if( get_row_layout() == 'staff_member' ): // start staff row ?>
<div class="staff">
<div class="swrap">
<?php if( get_sub_field ('name')): // get staff name ?>
<h3><?php the_sub_field('name'); ?></h3>
<?php endif; ?>
</div><!-- end swrap -->
</div>
<?php elseif( get_row_layout() == 'department_layout' ): //start department row ?>
<div class="dep">
<?php if( get_sub_field ('department')): // get dep head ?>
<h2><?php the_sub_field('department'); ?></h2>
<?php endif;
if( get_sub_field('department_text') ): // get dep text
the_sub_field('department_text');
endif; ?>
</div>
<?php endif;
endwhile; ?>
</div><!-- end staff-content -->
<?php else :
// no layouts found
endif;
}
Because you have different types of layouts the solution would be to keep a running total of each type of layout.
$layout_counts = array();
// ... some time later
while (have_rows('staff_content')) {
the_row();
$layout = get_row_layout();
if (!isset($layout_counts[$layout])) {
$layout_counts[$layout] = 0;
}
$layout_counts[$layout]++;
$class = 'even';
if ($layout_counts[$layout] % 2) {
$class = 'odd';
}
//... some time later
if ($layout == 'staff_member') {
?>
<div class="staff <?php echo $class; ?>">
/// etc...
</div>
<?php
}
}
Thanks John. That’s exactly what I needed.