[英]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.