DEV Community

Discussion on: Wordpress: ACF Repeater & Flexible Content fields

Collapse
 
kyleakelly profile image
Kyle Kelly

I may be misunderstanding your requirements, but the built in ACF functions should handle this for you.

If you're using a flexible content field use something like this to loop through the fields and pick out the layout type:

<?php

// check if the flexible content field has rows of data
if( have_rows('flexible_content_field_name') ):

     // loop through the rows of data
    while ( have_rows('flexible_content_field_name') ) : the_row();

        if( get_row_layout() == 'paragraph' ):

            the_sub_field('text');

        elseif( get_row_layout() == 'download' ): 

            $file = get_sub_field('file');

        endif;

    endwhile;

else :

    // no layouts found

endif;

?>

In this example the flexible_content_field_name is the name of the field you setup in ACF and the if/else-if area is where you place your layout code to handle the various flexible content layouts you may have for this field.

Based on your screenshot though, you may not need a flexible layout if the type of content will be the same, just the number of items will be variable then the repeater field should do the job.

If that's the case then the example code should do the trick, I'll customize it to your screenshot:

<?php

// check if the repeater field has rows of data
if( have_rows('field_name_goes_here') ):

    // loop through the rows of data
    while ( have_rows('field_name_goes_here') ) : the_row();

        // display a sub field value
        ?><h3><?php the_sub_field('title'); ?></h3>
          <p><?php the_sub_field('paragraph'); ?></p>
<?php
    endwhile;

else :

    // no rows found

endif;

?>

If I misunderstood completely, please let me know.

Collapse
 
leanminmachine profile image
leanminmachine • Edited

hey there Kyle! thank you for your reply.
Hmm, yeah I think maybe you misunderstood the use case a little.

I wouldn't know the row layout name will be called 'paragraph'. Cos the client would be creating their own rows. Or adding more rows / removing pre-existing ones. Hence I need something that can iterate through pretty generically without a certain row name ;)