Support

Account

Home Forums Add-ons Flexible Content Field Loop with Counter for Flexible Content Field Sub Field Output

Solved

Loop with Counter for Flexible Content Field Sub Field Output

  • Forgive me if a more general PHP question doesn’t belong here but I know there’s a better way to do this and I’ve tried to no avail. Thanks in advance for any help!

    <?php if(get_row_layout() == '3_column_layout'): ?>
    
    	<div class="row">
    		
    		<?php if(get_sub_field('cb_3_col_main_header')): ?>
    		
    			<div class="small-12 columns">
    				<h2><?php the_sub_field('cb_3_col_main_header'); ?></h2>
    			</div>
    			
    		<?php endif; ?>
    		
    		<div class="small-12 large-4 columns">
    			<dl>
    				<?php if(get_sub_field('cb_3_col_1_link')): ?>
    					<dt><a href="<?php the_sub_field('cb_3_col_1_link'); ?>"><?php the_sub_field('cb_3_col_1_header'); ?></a></dt>
    				<?php else: ?>
    					<dt><?php the_sub_field('cb_3_col_1_header'); ?></dt>
    				<?php endif; ?>
    				<dd><?php the_sub_field('cb_3_col_1_copy'); ?></dd>
    			</dl>
    		</div>
    		
    		<div class="small-12 large-4 columns">
    			<dl>
    				<?php if(get_sub_field('cb_3_col_2_link')): ?>
    					<dt><a href="<?php the_sub_field('cb_3_col_2_link'); ?>"><?php the_sub_field('cb_3_col_2_header'); ?></a></dt>
    				<?php else: ?>
    					<dt><?php the_sub_field('cb_3_col_2_header'); ?></dt>
    				<?php endif; ?>
    				<dd><?php the_sub_field('cb_3_col_2_copy'); ?></dd>
    			</dl>
    		</div>
    			
    		<div class="small-12 large-4 columns">
    			<dl>
    				<?php if(get_sub_field('cb_3_col_3_link')): ?>
    					<dt><a href="<?php the_sub_field('cb_3_col_3_link'); ?>"><?php the_sub_field('cb_3_col_3_header'); ?></a></dt>
    				<?php else: ?>
    					<dt><?php the_sub_field('cb_3_col_3_header'); ?></dt>
    				<?php endif; ?>
    				<dd><?php the_sub_field('cb_3_col_3_copy'); ?></dd>
    			</dl>	
    		</div>
    		
    	</div>
    
    <?php endif; ?>
    • Elliot

    • October 15, 2013 at 4:31 pm

    Hi @brian Larson

    I don’t understand the question from just the title, you will need to explain the issue with more detail.

  • 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; ?>
    • Elliot

    • October 16, 2013 at 4:31 pm

    Hi @brian Larson

    Thanks for the explanation, I see what you are trying to do. Perhaps this is what you are after:

    
    <?php if(get_row_layout() == '3_column_layout'): 
    	
    	// vars
    	$max = 3;
    	
    	?>
    	<div class="row">
    		
    		<?php for( $i = 1; $i <= $max; $i++ ); ?>
    		<div class="small-12 large-4 columns">
    			<dl>
    				<dt><?php the_sub_field("cb_{$max}_col_{$i}_header"); ?></dt>
    				<dd><?php the_sub_field("cb_{$max}_col_{$i}_copy"); ?></dd>
    				<dd><?php the_sub_field("cb_{$max}_col_{$i}_link"); ?></dd>
    			</dl>
    		</div>
    		<?php endfor; ?>
    		
    	</div>
    
    <?php endif; ?>
    

    Untested, but should work

  • Perfect! It was the “abc{$d}efg” variable within a string syntax with the curly braces that I totally forgot about. Huge help. Thanks Elliot! U rule. This we know.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.