1

Using ACF (Advanced Custom Fields), I set up a field (slides) with two subfields (title) and (slide). In my header.php file, I have this code which outputs a list of the title subfield.

// header.php //
<nav id="site-navigation" class="main-navigation" role="navigation">
    <?php $frontpage_id = get_option('page_on_front'); ?>
    <?php if ( is_singular() && have_rows('slides', $frontpage_id) ): ?>
        <ul>
            <?php while ( have_rows('slides', $frontpage_id) ) : the_row(); ?>
                <li class="group"><a href="#"><?php the_sub_field('title'); ?></a></li>
            <?php endwhile;?>
        </ul>
    <?php endif; ?>
</nav>

Then in my index.php file, I have div.content. In this div, I would like to output a list of the corresponding slide subfield. How can I do this?

// index.php //
<div class="content"></div>
J82
  • 8,267
  • 23
  • 58
  • 87

1 Answers1

0

You could repeat a similar loop in the index.php file -

<?php 
$frontpage_id = get_option('page_on_front');

if( have_rows('slides', $frontpage_id) ): 
    while( have_rows('slides', $frontpage_id) ) : the_row();

        // if your 'slide' field is an image
        $slide = get_sub_field('slide'); ?>

        <img src="<?php echo $slide['url']; ?>" alt="<?php echo $slide['alt']; ?>" />


    <?php endwhile;
endif;
?>

Or you could grab the entire array of data associated with the repeater using get_field('slides', $frontpage_id); - set that as a global variable and then use a foreach loop in both files. I think the first option is easier.

johnnyd23
  • 1,665
  • 2
  • 13
  • 24