Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Can you loop through different fields on a single page with Advanced Custom Fields?

I have a masthead.php and a front-page.php. In my front-page.php I'm using "get_template|_parts" to include the masthead.php onto the page. However, for whatever reason, I'm only getting the masthead, but no content from front-page.php. However, when I comment out the "get_template_parts" function, my content displays, although I lose my masthead. I can't figure out why. I've been super careful with my AFC setup.

On my AFC set up, I have "masthead_text" set up as a reapeater field, and all the sub-fields within that repeater are text fields. "header_carousel" is just a simple gallery field. "flexible_content" is a repeater field, that has mostly text fields within it and an image field.

There seems to be a conflict between the two files. I've tried everything I can think of.

MASTHEAD.PHP

<header>
<?php 
    $defaults = array (
        'theme_location' => 'main-navigation',
        'menu_class' => 'no-bullet',
        'container' => false
    ); ?>

        <nav class="main-header-nav">
            <img src="<?php echo get_template_directory_uri();?>/icons/Hilton-logo.svg" alt="Hilton Logo" class="header-logo">
            <?php  wp_nav_menu($defaults); ?>
        </nav>


<?php if( have_rows('masthead_text') ): ?>



    <?php while( have_rows('masthead_text') ): the_row(); 

        // vars
        $topMastHeader = get_sub_field('top_header');
        $bottomMastHeader = get_sub_field('bottom_header');

     ?>


        <h3 class="header-text tagline"><?php echo $topMastHeader; ?></h3>
        <h1 class="header-text"><?php echo $bottomMastHeader; ?></h1>

    <?php endwhile; endif; ?>


<?php 

$mastHeadImages = get_field('header_carousel');

if( $mastHeadImages ) { ?>
    <div class="cycle-slideshow carousel-container"  data-cycle-fx="scrollHorz" data-cycle-timeout="0" data-cycle-prev="#prev" data-cycle-next="#next" data-cycle-caption=".carousel-counter" data-cycle-caption-template="<span class='bigger'>{{slideNum}}/ </span> <span class='smaller'>{{slideCount}}</span>">
       <div class="cycle-caption"></div>
        <?php $i = 0; ?>
        <?php foreach( $mastHeadImages as $image ): ?>
                     <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" class="carousel-container__img carousel-img-<?php echo $i; ?>" />      
        <?php $i++; endforeach; ?>
        <div class="masthead_overlay"></div>
    </div>
    <div class="center carousel-control-container">
    <a href="#" id="prev">
        <span class="icon-arrow-left"></span>
    </a> 
    <a href="#" id="next">
        <span class="icon-arrow-right"></span>
    </a>
    <div class="carousel-counter">

    </div>
</div>


<?php } ?>
</header>
FRONT-PAGE.PHP

<?php
/*
 * Template Name: Updated Custom Homepage
 */
 get_header(); ?>

 <?php get_template_part('masthead'); ?>

 <div id="main" role="main">


   <?php if( have_rows('flexible_content') ): ?>



    <?php while( have_rows('flexible_content') ): the_row(); 

        // vars
        $mainContentImage = get_sub_field('main_content_image');
        $asideContentImage = get_sub_field('aside_content_image');
        $mainContentText = get_sub_field('main_content_text');
        $asideContentText = get_sub_field('aside_content_text');
        $CTALink = get_sub_field('cta_link');
        $CTAText = get_sub_field('cta_text');
        $topMastHeader = get_sub_field('top_mast_header');
        $bottomMastHeader = get_sub_field('bottom_mast_header');



     ?>

    <main id="main-content-container">

<!-- main content start-->

        <div class="main-content" style="background-image: url(<?php echo $mainContentImage['url']; ?>)">


            <div class="main-content-text">
                <h3>
                    <?php echo $mainContentText; ?>
                </h3>
                <p>
                    Sub Text
                </p>
                <?php if($CTALink) :?>
                <a href="<?php echo $CTALink; ?>" class="main-cta-btn">
                    <?php echo $CTAText; ?>
                </a>
                <?php endif;?>
            </div>

        </div>



<!-- main content end -->

<!--Aside content start-->

      <div class="aside-content" style="background-image: url(<?php echo $asideContentImage['url']; ?>)">

        <h2 style="color: #fff;"><?php echo $asideContentText; ?></h2> 

      </div>

<!--Aside content end-->


     </main>

    <?php endwhile; ?>

<?php endif; ?>

</div>



<?php get_footer();?>