[英]If (is_page_template()) not working in header.php
我有 3 个单独的 header 选项,所有选项都带有横幅图像:1. 主页 2. 赞助商模板 3. 所有其他页面。
我已将以下代码放在 header 中。 主页和所有其他页面都按预期工作,但我似乎无法使赞助商模板工作(class="sponsor-title" 没有出现)。
<?php if ( has_post_thumbnail()) : ?>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
<?php if ( is_front_page()): ?>
<span class="home"><h1><?php echo event_title(); ?></h1></span>
<span class="tag-line"><?php the_field('tag_line'); ?></span>
<span class="date"><?php the_field('date_time_header'); ?></span>
<?php
$ticket = get_field('ticket_url');
if ( $ticket ):
$ticket_url = $ticket['url'];
$ticket_title = $ticket['title'];
?>
<a class="button" href="<?php echo esc_url($ticket_url); ?>"><?php echo esc_html($ticket_title); ?></a>
<?php if (!is_page_template('page-templates/all-sponsor-template.php')); ?>
<span class="sponsor-title"><h1><?php echo event_title(); ?></h1></span>
<?php endif; ?>
<?php else: ?>
<span class="page-title"><h1><?php the_field('page_header'); ?></h1></span>
<span class="sub-header"><?php the_field('sub_header'); ?></span>
<?php endif;?>
我做错了什么? 我想确保在选择模板或页面时出现正确的样式,因为它与其他页面非常不同。
<?php if (!is_page_template('page-templates/all-sponsor-template.php')); ?>
// Stuff
<?php endif; ?>
在这里,您正在检查页面模板是否不是page-templates/all-sponsor-template.php
,如果不是 - 那么您显示赞助商标题...
我可能会感到困惑,但不应该是这样吗?
<?php if (is_page_template('page-templates/all-sponsor-template.php')); ?>
// Stuff
<?php endif; ?>
即交换 if 条件,以便赞助商标题显示页面模板是否是提到的文件。
编辑:检查是否正在使用赞助商页面模板的 if 语句位于检查当前页面是否为主页的 if 语句中。
如果您将模板检查上移一级,它应该可以解决问题:
<?php if ( has_post_thumbnail()) : ?>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
<?php if ( is_front_page()): ?>
<span class="home"><h1><?php echo event_title(); ?></h1></span>
<span class="tag-line"><?php the_field('tag_line'); ?></span>
<span class="date"><?php the_field('date_time_header'); ?></span>
<?php
$ticket = get_field('ticket_url');
if ( $ticket ):
$ticket_url = $ticket['url'];
$ticket_title = $ticket['title'];
?>
<a class="button" href="<?php echo esc_url($ticket_url); ?>"><?php echo esc_html($ticket_title); ?></a>
<?php endif; ?>
<?php if (is_page_template('page-templates/all-sponsor-template.php')); ?>
<span class="sponsor-title"><h1><?php echo event_title(); ?></h1></span>
<?php endif; ?>
<?php else: ?>
<span class="page-title"><h1><?php the_field('page_header'); ?></h1></span>
<span class="sub-header"><?php the_field('sub_header'); ?></span>
<?php endif;?>
这样,它将检查赞助商模板是否正在所有页面上使用,而不仅仅是在主页上。
你的问题是你的if
语句的语法。 试试这个:
<?php if (!is_page_template('page-templates/all-sponsor-template.php')) { ?>
<span class="sponsor-title"><h1><?php echo event_title(); ?></h1></span>
<?php } else { ?>
<span class="page-title"><h1><?php the_field('page_header'); ?></h1></span>
<span class="sub-header"><?php the_field('sub_header'); ?></span>
<?php } ?>
我总是使用{..}
括号,因为它更容易理解和理解代码的逻辑。 但是,要使用您尝试过的if
语句的正确结构重写您的代码,它看起来像这样:
<?php if (!is_page_template('page-templates/all-sponsor-template.php')): ?>
<span class="sponsor-title"><h1><?php echo event_title(); ?></h1></span>
<?php else: ?>
<span class="page-title"><h1><?php the_field('page_header'); ?></h1></span>
<span class="sub-header"><?php the_field('sub_header'); ?></span>
<?php endif; ?>
当然,这里还有感叹号:
if (!is_page_template('page-templates/all-sponsor-template.php'))
表示“如果不是页面模板 all-sponsor-template.php”,因此如果您检查 TRUE,请将其删除。
最后,请记住,由于某些全局变量在循环期间被覆盖, is_page_template()
在循环中将不起作用。 但是,如果此代码在您的header.php
中,那么您应该没问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.