简体   繁体   English

如果 (is_page_template()) 在 header.php 中不起作用

[英]If (is_page_template()) not working in header.php

I have 3 separate header options, all with a banner image: 1. Homepage 2. Sponsor Template 3. All other pages.我有 3 个单独的 header 选项,所有选项都带有横幅图像:1. 主页 2. 赞助商模板 3. 所有其他页面。

I have placed the below code in the header.我已将以下代码放在 header 中。 The homepage and all other pages are working as expected, but I can't seem to make the Sponsor template work (the class="sponsor-title" is not appearing).主页和所有其他页面都按预期工作,但我似乎无法使赞助商模板工作(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;?> 

What have I done wrong?我做错了什么? I want to ensure that when the template or page is chosen that the correct styling appears, as it is very different from the other pages.我想确保在选择模板或页面时出现正确的样式,因为它与其他页面非常不同。

<?php if (!is_page_template('page-templates/all-sponsor-template.php')); ?>
    // Stuff
<?php endif; ?>

Here you are checking if the page template is NOT page-templates/all-sponsor-template.php , and if it isn't - then you show the sponsor title...在这里,您正在检查页面模板是否不是page-templates/all-sponsor-template.php ,如果不是 - 那么您显示赞助商标题...

I might be confused, but shouldn't it be like this?我可能会感到困惑,但不应该是这样吗?

<?php if (is_page_template('page-templates/all-sponsor-template.php')); ?>
    // Stuff
<?php endif; ?>

Ie swapping the if condition so that the sponsor title shows if the page template IS the mentioned file.即交换 if 条件,以便赞助商标题显示页面模板是否是提到的文件。

Edit: The if statement that checks if the sponsor page template is being used, is inside the if statement that checks if the current page is the home page.编辑:检查是否正在使用赞助商页面模板的 if 语句位于检查当前页面是否为主页的 if 语句中。

If you move the template check one level up, it should do the trick:如果您将模板检查上移一级,它应该可以解决问题:

<?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;?> 

This way it will check if the sponsor template is being used on all pages and not only when on the home page.这样,它将检查赞助商模板是否正在所有页面上使用,而不仅仅是在主页上。

Your problem is the syntax of your if statement.你的问题是你的if语句的语法。 Try this instead:试试这个:

<?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 } ?>

I always use the {..} brackets, as it's easier to follow and understand the logic of the code.我总是使用{..}括号,因为它更容易理解和理解代码的逻辑。 However, to rewrite your code using the correct structure of the if statement you've attempted, it would look like this:但是,要使用您尝试过的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; ?>

And, of course, the exclamation mark here:当然,这里还有感叹号:

if (!is_page_template('page-templates/all-sponsor-template.php'))

means "If NOT page template all-sponsor-template.php" so remove it if you are checking for TRUE.表示“如果不是页面模板 all-sponsor-template.php”,因此如果您检查 TRUE,请将其删除。

Finally, keep in mind that due to certain global variables being overwritten during The Loop, is_page_template() will not work in The Loop.最后,请记住,由于某些全局变量在循环期间被覆盖, is_page_template()在循环中将不起作用。 However, if this code is in your header.php , then you should be fine.但是,如果此代码在您的header.php中,那么您应该没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM