Is there a way to list all sub fields in a repeater field or a flexible field? It would be great to know this as there is A LOT that can be done programmatically. With this information.
Hi @chriscarvache
You can just do this:
$repeaterfield = get_field('repeaterfieldname');
and it will contain all the sub fields as an associative array .
That should do it for you 🙂
I tried this and just get the word “Array” instead of my list of [repeated] fields for that Post.
Here’s what worked: using the examples found on this page:
http://www.advancedcustomfields.com/resources/repeater/
I created the following code that checks my repeater field for that Post and if it finds rows, list (wrapped in li tags) each row. I only have one field (subfield) per row, so my example is very basic….if your rows contain tabular-style data with more than one field per row, you’ll have to more closely follow the examples from that page.
Here’s my code that works:
<?php if( have_rows('room_types') ): ?>
<ul>
<?php while( have_rows('room_types') ): the_row(); ?>
<li><?php the_sub_field('room_type'); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Hopefully this will help. 🙂
@trisham
What you’re doing is not the same as what @chriscarvache is looking for tho.
And fetching the repeater field into an array does work, I’ve done it many many times 🙂
However I might have to explain it with a bit more code:
<?php
$repeater = get_field('repeaterfieldname');
//make sure atleast 1 row exists
if( $repeater ){
/* $repeater contains something like this, where each inner array represents a single row.
array(
[0] => array(
'subfield1' => 'value',
'subfield2' => 'value'
),
[1] => array(
'subfield1' => 'value',
'subfield2' => 'value'
),
[2] => array(
'subfield1' => 'value',
'subfield2' => 'value'
)
)
*/
foreach( $repeater as $row ){
//$row now contains all the subfields in this row of the repeater..
print_r($row);
}
}
?>