Support

Account

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

Solved

Unique ID's for each layout row

  • Hi, i’m just experimenting with Flexible Fields and wonder if this is possible?

    I’m using the demo code from here: http://www.advancedcustomfields.com/resources/field-types/flexible-content/

    <?php 
     
    /*
    *  Loop through a Flexible Content field and display it's content with different views for different layouts
    */
     
    while(has_sub_field("content")): ?>
     
    	<?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; ?>

    Say when administering the site then, i enter content such as:

    • Paragraph
    • File
    • Paragraph
    • Featured Posts
    • File
    • Featured Posts
    • Paragraph

    How would i output a unique number attached to each instance of a layout? ie

    • Paragraph (1st instance of paragraph)
    • File (1st instance of file)
    • Paragraph (2nd instance of paragraph)
    • Featured Posts (1st instance of featured posts)
    • File (2nd instance of file)
    • Featured Posts (2nd instance of featured posts)
    • Paragraph (3rd instance of paragraph)

    If that’s not possible is there a way to increment each layout instance with a unique number like this instead? ie

    • Paragraph (1st layout)
    • File (2nd layout)
    • Paragraph (3rd layout)
    • Featured Posts (4th layout)
    • File (5th layout)
    • Featured Posts (6th layout)
    • Paragraph (7th layout)

    Hope that makes sense! Thanks in advance 🙂

  • 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; ?>
    
  • Thank you very much Elliot, worked perfectly 🙂

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

The topic ‘Unique ID's for each layout row’ is closed to new replies.