Home › Forums › Add-ons › Flexible Content Field › Loop with Counter for Flexible Content Field Sub Field Output › Reply To: Loop with Counter for Flexible Content Field Sub Field Output
Sorry for the lack of detail Elliot and thanks for the reply. This isn’t an ACF issue. It’s more of a general PHP question and I was hoping to tap the ACF community. If that sort of a question doesn’t belong here let me know and I can post elsewhere. That being said…
The code above works. I just know there’s a better way to do it and I couldn’t figure it out. The 3 sets of “column” field names (examples: cb_3_col_main_header, cb_3_col_1_link) are templated so to speak with the second number going from 1-3 within each column set. I should have done this in the first place but below I’ve simplified the code where you can see how the sub field names increment 1-3 within each <div> .columns set. Is there a way to do this with a loop, array(s) and a counter so the _1_, _2_, _3_ portions of the field names are all dynamic.
Wow this feels like I’m not doing a good job of explaining. 😐 Hopefully the simplified code below is much easier in terms of demonstrating my question.
<?php if(get_row_layout() == '3_column_layout'): ?>
<div class="row">
<div class="small-12 large-4 columns">
<dl>
<dt><?php the_sub_field('cb_3_col_1_header'); ?></dt>
<dd><?php the_sub_field('cb_3_col_1_copy'); ?></dd>
<dd><?php the_sub_field('cb_3_col_1_link'); ?></dd>
</dl>
</div>
<div class="small-12 large-4 columns">
<dl>
<dt><?php the_sub_field('cb_3_col_2_header'); ?></dt>
<dd><?php the_sub_field('cb_3_col_2_copy'); ?></dd>
<dd><?php the_sub_field('cb_3_col_2_link'); ?></dd>
</dl>
</div>
<div class="small-12 large-4 columns">
<dl>
<dt><?php the_sub_field('cb_3_col_3_header'); ?></dt>
<dd><?php the_sub_field('cb_3_col_3_copy'); ?></dd>
<dd><?php the_sub_field('cb_3_col_3_link'); ?></dd>
</dl>
</div>
</div>
<?php endif; ?>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.