【术语】:条件标签(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}}',
),
代码高亮:
代码高亮