Support

Account

Home Forums Add-ons Flexible Content Field Unique ID's for each layout row Reply To: Unique ID's for each layout row

  • Hi @kurdt_the_goat

    Both of these outputs are possible with PHP. Please see below for the first example:

    
    <?php 
     
    /*
    *  Loop through a Flexible Content field and display it's content with different views for different layouts
    */
    
    $counters = array();
     
    while(has_sub_field("content")): 
    
    	
    	// update counter
    	$layout = get_row_layout();
    	
    	if( !isset( $counters[ $layout ] ) )
    	{
    		// initialize counter
    		$counters[ $layout ] = 1;
    	}
    	else
    	{
    		// increase existing counter
    		$counters[ $layout ]++
    	}
    
    	?>
     	
     	<p><?php echo $layout; ?> instance #<?php echo $counters[ $layout ]; ?></p>
     	
    	<?php if(get_row_layout() == "paragraph"): // layout: Content ?>
     
    		<div>
    			<?php the_sub_field("content"); ?>
    		</div>
     
    	<?php elseif(get_row_layout() == "file"): // layout: File ?>
     
    		<div>
    			<a href="<?php the_sub_field("file"); ?>" ><?php the_sub_field("name"); ?></a>
    		</div>
     
    	<?php elseif(get_row_layout() == "featured_posts"): // layout: Featured Posts ?>
     
    		<div>
    			<h2><?php the_sub_field("title"); ?></h2>
    			<?php the_sub_field("content"); ?>
     
    			<?php if(get_sub_field("posts")): ?>
    				<ul>
    				<?php foreach(get_sub_field("posts") as $p): ?>
    					<li><a href="<?php echo get_permalink($p->ID); ?>"><?php echo get_the_title($p->ID); ?></a></li>
    				<?php endforeach; ?>
    				</ul>
    			<?php endif; ?>
     
    		</div>
     
    	<?php endif; ?>
     
    <?php endwhile; ?>