Home › Forums › Add-ons › Flexible Content Field › Flexible Content "key"
When using a flexible content layout, I assume each layout added to the page has some sort of unique “key” value for it.
Is there a way to retrieve this value?
I am wanting to use it to create unique id’s for each item in the layout for styling purposes.
eg: $key = THE THING I AM MISSING
then…
echo '<div id="layout_'.$key.'">';
etc…..
Hi @frysteen
I believe you are looking for the get_row_layout()
function as seen in the flexible content documentation: http://www.advancedcustomfields.com/resources/field-types/flexible-content/
Thanks
E
Hi Elliot, I have all my layouts working fine and use the get_row_layout()
to retrieve the corresponding layout file with no dramas.
What I am after is a unique identifier for each layout module.
For example, if I have two Gallery modules on the same flexible content area, I assume each one has a different ID of some kind so that the images from Gallery A are not mixed with images from Gallery B.
So when looping over the modules, is there an ID associated with each module added in the back end?
something like this….
if( get_row_layout() == 'paragraph' ):
key_id = ??????
the_sub_field('text');
elseif( get_row_layout() == 'download' ):
key_id = ??????
$file = get_sub_field('file');
endif;
Hence, each Paragraph added to the page with have unique KEY for it.
I hope I am making sense and not missing something that is easy 🙂
Thanks, Sam
Hi @frysteen
The data is saved to the database using a numeric counter as the $key.
So you could achieve this on the front end by introducing a counter like so:
<?php
$i = -1;
while( have_rows() ) {
the_row();
$i++;
if( get_row_layout() == 'paragraph' ):
echo "key={$i}";
the_sub_field('text');
elseif( get_row_layout() == 'download' ):
echo "key={$i}";
$file = get_sub_field('file');
endif;
}
?>
I hope that helps.
Thanks
E
The topic ‘Flexible Content "key"’ 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.