Home › Forums › Add-ons › Flexible Content Field › Unique ID's for each layout row › Reply To: Unique ID's for each layout row
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; ?>
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.