Ashley Cameron Design

Ashley Cameron Design

Shop the Creative Design Market

Good evening

ACF nested repeater within a repeater and flexible content for WordPress

This post was last updated: Oct 8, 2022
SnippetsWordPress

About a 1 minute read

"It’s not what you look at that matters, it’s what you see."

Henry David Thoreau


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/*
Template Name: Resources
*/


get_header(); ?>

<div class="container">
    <?php while(have_posts()) : the_post();?>
        <div class="entry resources col-md-11 col-md-offset-1">
            <h2><?php the_title();?></h2>
            <?php the_content();?>
           
            <?php // repeater #1
                if( have_rows('links') ): ?>
                <?php while( have_rows('links') ): the_row(); ?>
                   
                    <div class="col-md-6">
                       
                        <h3><?php the_sub_field('title'); ?></h3>
                        <div class="desc"><?php the_sub_field('description'); ?></div>
                        <?php // repeater #2
                            if( have_rows('files') ): ?>
                            <ul>
                            <?php while( have_rows('files') ): the_row(); ?>
                                <li>
                                  <?php // flexible content
                                    if( have_rows('link') ):
                                        while ( have_rows('link') ) : the_row();
                                            if( get_row_layout() == 'file' ):  
                                                $file = get_sub_field('file');
                                                $ext = pathinfo($file, PATHINFO_EXTENSION);?>
                                                <a target="_blank" rel="noopener noreferrer">" class="<?php echo $ext; ?>">
                                                    <?php the_sub_field('link_title'); ?>
                                                </a>
                                            <?php elseif( get_row_layout() == 'external_link' ): ?>
                                                <a target="
_blank" rel="noopener noreferrer">" class="ext">
                                                    <?php the_sub_field('link_title'); ?>
                                                </a>
                                            <?php endif;
                                        endwhile;
                                    else :
                                        // no layouts found
                                    endif; ?>
                                                           
                                </li>
                            <?php endwhile; ?>
                            </ul>
                    <?php endif; //if( get_sub_field('files') ): ?>
                    </div>
                                   
                <?php endwhile; ?>
            <?php endif; ?>
           
        </div><!-- .entry -->
    <?php endwhile; ?>
</div>
<?php get_footer(); ?>