[英]Php - What is the proper way to accomplish header/footer extraction from pages?
[英]Best way to omit elements from Wordpress header/footer on certain pages
我在Wordpress網站上的大多數頁面具有相同的頁眉和頁腳。 標題包括表單,菜單和聯系按鈕。 頁腳還包含一個表單。
我有許多頁面具有完全自定義的布局。 例如。 page-personal-training-landing.php和page-beach-body-ready.php。 對於這些頁面,我省略了頁眉和頁腳中的元素。
目前,我正在使用“ if not”語句在除那些自定義頁面之外的所有頁面上顯示這些元素。
<?php if ((!is_page( 'contact' ))&&(!is_page('beach-body-landing'))&&(!is_page('4-week-body-transformation'))&&(!is_page('beach-body-ready'))&&(!is_page('personal-training-landing'))){?>
<div class="contact-button"></div>
<?php } ?>
...但是您可以看到這段代碼變得很長,並且在創建新頁面時,我會不斷為其添加新頁面。
我以為模板是解決方案.. if(!is_page_template('noheadernofooter-page.php')...但不幸的是,Wordpress默認為該模板布局,然后默認為自定義頁面布局...
Wordpress頁面模板選擇的層次結構...
我現在在想,也許我應該使用“高級”自定義字段來添加一些復選框。 例如。 隱藏標題按鈕(是/否),然后查詢當前頁面上那些按鈕的值。
這聽起來是最合乎邏輯的解決方案嗎?
有誰知道在沒有其他插件的情況下通常如何做?
謝謝
我認為我在這里找到了最優雅的解決方案...
https://stackoverflow.com/a/36521754/3656408
“您可以創建新的頁眉和頁腳文件,分別說“ header-mytemplate.php”和“ footer-mytemplate.php”。創建所需的結構。在新模板中,調用這些頁眉和頁腳,例如get_header ('mytemplate');和get_footer(mytemplate);“。
如果有人認為有更好的解決方案,我仍然想知道
我認為,您的做法已經是正確的方法。 我可能會添加include以使其更易於維護,但是在我看來,如果要采用聲明的方式。 以我使用的一些代碼(為OP修改)為例:
if( (is_page( 'contact' ) ) {
include( 'header-contact.php' ); // include this file on contact page
} elseif( is_page('beach-body-landing') ) {
include( 'header-body-landing.php' ); // include that file on body landing page
} else {
get_header(); // otherwise use our normal header
}
不幸的是,沒有鈎子可以插入get_header()
, get_footer()
或get_sidebar()
,因為那樣我們就可以在一個地方完成所有操作。 這只是Wordpress開發的本質。
編輯
要包含您在下面提到的內容:您可以這樣調整:
if( (is_page( 'contact' ) ) {
get_header('contact'); // use `header-contact.php`
} elseif( is_page('beach-body-landing') ) {
get_header('body-landing'); // use `header-body-landing.php`
} else {
get_header(); // otherwise use our normal header
}
希望這可以幫助!
這是另一種利用get_header()
方式,如前所述:
獲取帖子塊並將其傳遞到get_header中。 這樣,您的頁面將始終尋找自定義的頭文件,但是如果找不到,則WP將默認為標准的header.php:
if ( is_page() ) {
global $post;
get_header( $post->post_name );
}
這確實意味着您將需要為每個頁面自定義header.php文件,因此最終可能會有數百個頭文件,但是您可以使用符號鏈接來規避該問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.