Home › Forums › Add-ons › Flexible Content Field › 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:
How would i output a unique number attached to each instance of a layout? ie
If that’s not possible is there a way to increment each layout instance with a unique number like this instead? ie
Hope that makes sense! Thanks in advance 🙂
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; ?>
The topic ‘Unique ID's for each layout row’ is closed to new replies.
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.