php - 如果 (is_page_template()) 在 header.php 中不起作用
问题描述
我有 3 个单独的标题选项,都带有横幅图片: 1. 主页 2. 赞助商模板 3. 所有其他页面。
我已将以下代码放在标题中。主页和所有其他页面都按预期工作,但我似乎无法使赞助商模板工作(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;?>
我做错了什么?我想确保在选择模板或页面时出现正确的样式,因为它与其他页面非常不同。
解决方案
你的问题是你的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
,那么您应该没问题。
推荐阅读
- three.js - Threejs WebXR控制器检测手
- c++ - 控制台应用程序的Qt自动测试
- postgresql - 是否可以在 Postgres INSERT 中声明和使用 $1、$2 等值?
- android - 如何发送 Multipart.Part 意图
- c - 调用使用命令行参数的函数
- typescript - 是否可以使用 for 循环内的条件缩短打字稿中的三元运算符
- elasticsearch - 在 LogStash 中,如何删除任何大于特定大小的 json/xml 字段
- java - WildFly:此 URL 不支持 HTTP 方法 POST
- r - 如何聚合数据框并通过 r 中的重复行对列的值求和
- iframe - 功能策略 Safari - iframes - Youtube - ReactJS