0

I have a page template that has a conditional statement to use get_template_part() to pull in a different template based on an ACF field value. The code from both template part files works outside of the conditional statement, but not when used inside the conditional switch statement - I.e if I paste the contents of the two template part files straight into the main page template, the code works as expected. Can anyone explain why this doesn’t work? Any help much appreciated!

Main Template code:

<?php /* Template Name: Content Block Page Template */ get_header(); ?>

<?php
    $theTitleLowerCase = strtolower(get_the_title());
    $theTitleHyphenatedAndLowerCase = preg_replace('#[ -]+#', '-', $theTitleLowerCase);
?>

<?php 
    $pageBackgroundImageUrl = get_field('page_background');
    $pageBackgroundColor = get_field('page_background_color');
    $invertTextColorTrueFalse = get_field('invert_text_color');
    if ($invertTextColorTrueFalse == 1) {
        $inverttextColorClass = " invert-text-color";
    } else {
        $inverttextColorClass = "";
    };
?>

<div class="jumbotron">
    <?php get_template_part( 'content', 'jumbotron' ); ?>
</div>

<span class="b-header--sticky__scroll-check"></span>
<header class="b-header--sticky">
    <div class="l-container">
        <nav class="desktop-nav" role="navigation">
            <?php html5blank_nav(); ?>
        </nav>
    </div>
</header>

<main role="main">
    <?php while( have_rows('add_content_block') ): the_row(); 
        $blockSelector = get_sub_field('select_content_block');
    ?>

    <?php 
        switch ($blockSelector) {
            case "Speaker Block":
                get_template_part( 'content', 'speakers' );
                break;
            case "Events Block":
                get_template_part( 'content', 'events' );
                break;
            default:
                echo "Default";
        }
    ?>

    <?php endwhile; ?>

</main>

<?php get_footer(); ?>

In the templates called by get_template_part(), I have the following code:

“Speakers Template”:

<div class="page-wrap content-template-inner-page-speakers-php<?php echo $inverttextColorClass; ?>" style="background-image: url(<?php echo $pageBackgroundImageUrl; ?>); background-color: <?php echo $pageBackgroundColor; ?>;" id="<?php echo $theTitleHyphenatedAndLowerCase; ?>">
<h1>Speaker Template Baby</h1>
<section>
    <div class="l-container">
        <div class="speakers speakers-block">
            <!-- Keynote Speakers -->
            <?php
                $args3 = array(
                    'post_type' => 'the_speakers',
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'speakers_taxonomies',
                            'terms' => 'keynote-speaker',
                            'field' => 'slug'
                        )
                    )
                );
                $theKeynoteSpeakers = new WP_Query($args3);
            ?>
            <div class="keynote-speakers l-grid">
                <?php while ($theKeynoteSpeakers->have_posts()) : $theKeynoteSpeakers->the_post(); ?>
                    <?php  
                        $speakerHeading = get_the_title();
                        $speakerSubHeading = get_field('speaker_job_title');
                        $speakerBio = get_field('speaker_bio');
                        $speakerImage = get_field('speaker_profile_image');
                        $speakerCompanyImage = get_field('speaker_company_logo');
                    ?>
                    <div class="l-grid__item l-grid__item--4 l-grid__item--lap-6 l-grid__item--palm-12 speaker">
                        <div class="speaker__image background-cover" style="background-image: url(<?php echo $speakerImage; ?>);"></div>
                        <div class="speaker__info"> 
                            <h3 class="speaker__heading">
                                <?php echo $speakerHeading; ?>
                            </h3>
                            <h5 class="speaker__sub-heading">
                                <?php echo $speakerSubHeading; ?>
                            </h5>
                            <a href="#0" class="b-button modal-opener speaker-modal-opener">See More</a>
                            <div class="speaker__modal b-screen b-screen--modal b-screen--modal--off-screen">
                                <div class="speaker__modal__inner b-screen__vertical-aligner">
                                    <i class="b-screen__modal-closer">X</i>
                                    <div class="l-grid">
                                        <div class="l-grid__item l-grid__item--4">
                                            <div class="speaker__image background-cover" style="background-image: url(<?php echo $speakerImage; ?>);"></div>
                                            <?php if ( $speakerCompanyImage ) :?>
                                                <div class="speaker__company-image-holder">
                                                    <img class="speaker__company-image" src="<?php echo $speakerCompanyImage; ?>" alt="Company Logo">
                                                </div>
                                            <?php endif; ?>
                                        </div>
                                        <div class="l-grid__item l-grid__item--8 speaker__modal__info">
                                            <h3 class="speaker__heading">
                                                <?php echo $speakerHeading; ?>
                                            </h3>
                                            <h5 class="speaker__sub-heading">
                                                <?php echo $speakerSubHeading; ?>
                                            </h5>
                                            <div class="speaker__bio">
                                                <?php echo $speakerBio; ?>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endwhile; ?>
            </div>

            <div class="text-center speakers-see-more">
                <a href="<?php echo home_url(); ?>/speakers" class="b-button">See More</a>
            </div>
            <div class="text-center show-more-speakers">
                <a href="#0" class="b-button show-more-speakers__btn">See More</a>
            </div>
        </div><!-- Speakers -->
    </div>
</section>
</div>

“Events Template Part”:

<div class="page-wrap content-template-inner-page-agenda-php<?php echo $inverttextColorClass; ?>" style="background-image: url(<?php echo $pageBackgroundImageUrl; ?>); background-color: <?php echo $pageBackgroundColor; ?>;" id="<?php echo $theTitleHyphenatedAndLowerCase; ?>">
<h1>Events Template Baby</h1>
<section id="agenda" class="pos-rel">
    <div class="l-container">

        <div class="events">
            <?php
                $args1 = array(
                    'post_type' => 'events',
                    'order' => 'ASC',
                    'orderby' => 'meta_value',
                    'meta_key' => 'event_start_time'
                );
                $nykEvents = new WP_Query($args1);
            ?>

            <ul class="b-list dis-blo agenda-block">
                <?php while ($nykEvents->have_posts()) : $nykEvents->the_post(); ?>

                    <?php  
                        $eventTitle = get_the_title();
                        $eventDescription = get_field('event_description');
                        $eventStartDate = get_field('event_start_date');
                        $eventEndDate = get_field('event_end_date');
                        $eventStartTime = get_field('event_start_time');
                        $eventEndTime = get_field('event_end_time');
                        $eventSpeakers = get_field('event_speakers'); // Repeater
                        $postId = get_the_ID();
                        $eventType = get_the_terms($postId, 'event_type'); // Array of Taxonomies
                        $taxSlugArray = array();
                        foreach( $eventType as $eventSlug ) {
                            $taxSlugArray[] = $eventSlug->slug;
                        }
                    ?>

                    <li class="b-list__item l-grid l-grid--top agenda__item <?php foreach ( $taxSlugArray as $slug ) { echo "$slug "; }?>">
                        <div class="l-grid__item l-grid__item--4 agenda__time-type">
                            <span class="agenda__start-time"><?php echo $eventStartTime; ?></span>
                            <span class="agenda__time-breaker">to </span>
                            <span class="agenda__end-time"><?php echo $eventEndTime; ?></span>
                            <div class="agenda__type"></div>
                        </div>
                        <div class="l-grid__item l-grid__item--4 agenda__title-desc">
                            <h4 class="agenda__title"><?php echo $eventTitle; ?></h4>
                            <div class="agenda__desc"><?php echo $eventDescription; ?></div>
                        </div>
                        <div class="l-grid__item l-grid__item--4 agenda__speaker-info">
                            <?php while( have_rows('event_speakers') ): the_row(); 
                                $eventSpeakerObject = get_sub_field('event_speaker');
                                $eventSpeakerHeading = $eventSpeakerObject->post_title;
                                if($eventSpeakerObject){
                                    $eventSpeakerImage = get_field('speaker_profile_image', $eventSpeakerObject->ID);
                                    $eventSpeakerSubHeading = get_field('speaker_job_title', $eventSpeakerObject->ID);
                                    $eventSpeakerBio = get_field('speaker_bio', $eventSpeakerObject->ID);
                                    $eventSpeakerCompanyImage = get_field('speaker_company_logo', $eventSpeakerObject->ID);
                                }
                            ?>
                                <div class="speaker">
                                    <div class="speaker__image background-cover" style="background-image: url(<?php echo $eventSpeakerImage; ?>);"></div>
                                    <div class="speaker__info"> 
                                        <h3 class="speaker__heading">
                                            <?php echo $eventSpeakerHeading; ?>
                                        </h3>
                                        <h5 class="speaker__sub-heading">
                                            <?php echo $eventSpeakerSubHeading; ?>
                                        </h5>
                                        <a href="#0" class="b-button modal-opener speaker-modal-opener">See More</a>
                                        <div class="speaker__modal b-screen b-screen--modal b-screen--modal--off-screen">
                                            <div class="speaker__modal__inner b-screen__vertical-aligner">
                                                <i class="b-screen__modal-closer">X</i>
                                                <div class="l-grid">
                                                    <div class="l-grid__item l-grid__item--4">
                                                        <div class="speaker__image background-cover" style="background-image: url(<?php echo $eventSpeakerImage; ?>);"></div>
                                                        <?php if ( $eventSpeakerCompanyImage ) :?>
                                                            <div class="speaker__company-image-holder">
                                                                <img class="speaker__company-image" src="<?php echo $eventSpeakerCompanyImage; ?>" alt="Company Logo">
                                                            </div>
                                                        <?php endif; ?>
                                                    </div>
                                                    <div class="l-grid__item l-grid__item--8 speaker__modal__info">
                                                        <h3 class="speaker__heading">
                                                            <?php echo $eventSpeakerHeading; ?>
                                                        </h3>
                                                        <h5 class="speaker__sub-heading">
                                                            <?php echo $eventSpeakerSubHeading; ?>
                                                        </h5>
                                                        <div class="speaker__bio">
                                                            <?php echo $eventSpeakerBio; ?>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            <?php endwhile; ?>
                        </div>
                    </li>
                <?php endwhile; ?>
            </ul>
        </div><!-- Events -->

    </div>
</section>
</div>
2

The problem: The code never cleans up after its queries

Your queries are currently missing several things:

  • The code never checks that the query found any posts via if ( $q->have_posts() )
  • There are no calls to wp_reset_postdata

When you call either the_post or setup_postdata, you change the global $post variable. This can cause issues in any code that runs after the loop as the post context is no longer the post of the page, but the last post of the query loop.

Calling wp_reset_postdata should fix this, but it must only be called if the query found posts. If this isn't done, then this can cause issues or slowdown in nested loops, and other unanticipated behaviour.

Here is an idealised WP_Query loop:

$args = array(
    // ....
);
$q = new \WP_Query( $args );
if ( $q->have_posts() ) {
    while( $q->have_posts() {
        $q->the_post();
        // display post
    }
    wp_reset_postdata();
} else {
    echo 'no posts found';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.