我有一个页面模板,它有一个条件语句,可以使用get\\u template\\u part()根据ACF字段值拉入不同的模板。两个模板部件文件中的代码都在条件语句之外工作,但在条件切换语句内部使用时不会工作,即如果我将两个模板部件文件的内容直接粘贴到主页模板中,代码将按预期工作。有人能解释为什么这不起作用吗?非常感谢您的帮助!
主模板代码:
<?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(); ?>
在get\\u template\\u part()调用的模板中,我有以下代码:
“发言人模板”:
<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>
“事件模板部分”:
<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>