【術語】:條件標籤(Conditional Tag)
is_home() 和 is_front_page()等函數可以稱為 條件標籤。
【術語】:模板標籤
參考:
如果網站的主頁顯示設置「最新文章」(默認)的情況下,是可以通過is_home() 進行判斷此頁是否為主頁,但是如果設置成「一個靜態頁面」,is_home() 就不是代表此頁是首頁了。所以wordpress判斷當前頁面需要is_home() 和is_front_page() 配合使用。
說明:
1、如果「主頁顯示」設置為「您的最新文章」(默認):對於網站首頁,is_home() 返回true, is_front_page() 返回true, `。
2、如果「主頁顯示」設置為「一個靜態頁面」,首頁-靜態頁 的is_front_page() 返回true,
而is_home() 返回fault;此時設定的文章頁的is_home() 返回true,因為文章頁已不是首頁,is_front_page() 返回fault。
或者說:
當is_home() 返回true:此頁是一個最新文章列表頁;此時如果is_front_page() 返回true,則此頁是網站主頁;如果is_front_page() 返回fault,則此頁不是網站主頁,而是一個最新文章列表頁面。
當is_home() 返回fault,則此頁是一個靜態頁面而非文章列表頁,如果is_front_page() 返回ture,則此靜態頁面已經設置為首頁。
is_front_page() | is_home() | 對應頁面類型 |
---|---|---|
1 | 1 | 此頁是網站首頁,也是文章清單頁 |
1 | 0 | 此頁是網站首頁,但是一個靜態頁面,不是文章清單頁 |
0 | 1 | 此頁不是網站首頁,僅是一個文章清單頁 |
0 | 0 | X |
那麼判斷語句如何寫?
第一種情況(2017主題index.php文件):
<?php if ( is_home() && ! is_front_page() ) : ?>
//此頁僅是一個文章清單頁,不是網站的首頁。
第二種情況:
第三種情況(在2017主題,inc/template-tags.php)
/** * Checks to see if we』re on the homepage or not. */ function twentyseventeen_is_frontpage() { return ( is_front_page() && ! is_home() ); } //此頁是靜態首頁但不是文章清單頁。 此函數用於如下情況:
if ( ( is_single() || ( is_page() && ! twentyseventeen_is_frontpage() ) ) && has_post_thumbnail( get_queried_object_id() ) )
//是一個文章頁,或是靜態頁面,取非:意思就是此頁是文章清單頁
1
2
navigation-top.php
if ( ( twentyseventeen_is_frontpage() || ( is_home() && is_front_page() ) ) && has_custom_header() ) :
//首頁且是靜態頁 或 首頁且是文章清單頁
對於各種不同的模板文件建議如何設置:
1、front-page.php
is_front_page鐵定是1(不用判斷is_home,所以都不判斷任何)
2、home.php
這是文章清單頁(blog首頁),is_home鐵定是1,判斷 is_front_page:
返回1:是首頁時,返回0不是首頁時
3、index.php
主要模板文件,
是首頁:
is_home() && ! is_front_page()
如果is_front_page()=1,對應的模板文件是front-page.php
初始化主頁設置
// Define and register starter content to showcase the theme on new sites.
$starter_content = array(
// Default to a static front page and assign the front and posts pages.
'options' => array(
'show_on_front' => 'page',
'page_on_front' => '{{home}}',
'page_for_posts' => '{{blog}}',
),
代碼高亮:
代碼高亮