My goal:
To set a global layout style for each item in a repeater. Example: I want to create content cards each having a title, blurb, and thumbnail with the ability to set a class that will say how each card will appear. So I create flexible content:
Layout: Cards
Fields:
Card Style (Select field)
Card (Repeater field)
– Title (text)
– blurb (textarea)
– image (image)
<?php
...
if ( get_row_layout() == 'cards' ) :
if ( have_rows('card') ) :
while ( have_rows('card') ) : the_row();
if ( get_the_field('card_style') === 'stacked' ) :
echo '<article class="card card--stacked">';
else :
echo '<article class="card">';
endif;
endwhile; endif;
endif;
...
endif; ?>
However, when I try to get the value of the select menu from inside the repeater field it doesn’t recognize the get_the_field(‘card_style’).
The purpose of having the card style outside of the repeater is the client should be able to pick the style once for all cards instead of having to set it for each card individually.
Hi @glenn
That’s because the repeater field doesn’t have a field with “card_style” as the name. If you want to get the layout value from inside the repeater field, you need to store it in a variable first. It should be something like this:
if ( get_row_layout() == 'cards' ) :
$cart_style = get_sub_field('card_style');
if ( have_rows('card') ) :
while ( have_rows('card') ) : the_row();
if ( $cart_style === 'stacked' ) :
echo '<article class="card card--stacked">';
else :
echo '<article class="card">';
endif;
endwhile; endif;
endif;
endif;
I hope this makes sense 🙂