在 WordPress 主题开发中,is_home() 和 is_front_page() 是两个非常容易混淆的条件判断函数。
很多开发者会简单认为:
is_home()= 首页
is_front_page()= 首页
但实际上,这个理解是不准确的。
在 WordPress 内部:
is_home()表示 文章索引页(Posts Index)is_front_page()表示 网站访问入口首页(Front Page)
这两个概念在某些情况下重合,在某些情况下完全不同。
如果你正在开发:
- WordPress 企业主题
- SEO 标题模块
- 首页模板系统
- 自定义主题框架
- ACF 首页设置
那么理解它们的区别非常重要。
本文将从 WordPress Core 源码、后台阅读设置以及实际测试三个方面深入分析。
一、WordPress 后台「阅读设置」决定首页类型
进入:
WordPress 后台
↓
设置
↓
阅读
可以看到:
您的主页显示
有两个选择:
选项 1:您的最新文章
对应:
show_on_front = posts
表示:
网站首页就是文章列表。
选项 2:一个静态页面
对应:
show_on_front = page
表示:
网站首页由一个 Page 页面控制。
同时会出现:
主页:
[选择页面]
文章页:
[选择页面]
对应:
page_on_front
page_for_posts
两个核心字段:
| 字段 | 作用 |
|---|---|
| page_on_front | 网站首页页面 |
| page_for_posts | 文章列表页面 |
二、is_home() 到底代表什么?
很多人误解:
is_home()
表示:
当前页面是不是网站首页
实际上不是。
它表示:
当前 Query 是否为文章索引页(Posts Index)
也就是:
WordPress 正在显示:
文章列表
例如:
最新文章
文章归档
Blog 页面
从 WordPress Core 可以看到:
$this->is_home = true;
的条件:
if (
!(
$this->is_singular ||
$this->is_archive ||
$this->is_search ||
$this->is_feed ||
$this->is_404 ||
$this->is_admin
)
) {
$this->is_home = true;
}
简单理解:
如果当前请求不是:
- 单篇文章
- 页面
- 分类
- 搜索
- 404
那么默认认为:
这是文章首页
所以:
is_home()
更准确的翻译应该是:
是否为 Posts Index
而不是:
是否为网站首页
三、is_front_page() 的含义
再看:
is_front_page()
WordPress Core:
public function is_front_page() {
if (
'posts' === get_option('show_on_front')
&&
$this->is_home()
) {
return true;
} elseif (
'page' === get_option('show_on_front')
&&
get_option('page_on_front')
&&
$this->is_page(get_option('page_on_front'))
) {
return true;
}
return false;
}
这里非常关键。
is_front_page() 有两个来源。
四、第一种情况:最新文章作为首页
后台:
设置 → 阅读
主页显示:
● 您的最新文章
数据库:
show_on_front = posts
访问:
/
结果:
is_home() = true
is_front_page() = true
为什么?
因为源码:
if (
'sposts' === show_on_front
&&
is_home()
)
成立。
所以:
默认文章首页:
同时拥有两个身份:
Posts Index
+
Front Page
实际测试:
show_on_front:posts
page_on_front:0
page_for_posts:0
访问首页:
结果:
is_home:true
is_front_page:true
这是唯一一个两个函数同时为 true 的常见场景。
五、第二种情况:静态首页
后台:
主页显示:
● 一个静态页面
主页:首页
文章页:Blog
数据库:
show_on_front = page
page_on_front = 10
page_for_posts = 15
访问:
/
WordPress 会执行:
$this->is_page = true;
$this->is_home = false;
也就是说:
静态首页:
is_page = true
is_home = false
is_front_page = true
这里很多开发者容易错误:
认为:
首页一定 is_home=true
实际上:
静态首页不是文章索引。
它只是一个普通 Page。
六、第三种情况:Blog 页面
继续:
后台:
主页:首页
文章页:Blog
访问:
/blog/
这个页面虽然后台选择的是一个 Page:
但是 WordPress 会特殊处理。
结果:
is_home = true
is_front_page = false
is_page = false
原因:
Blog 页面不是普通页面。
它被 WordPress 转换成:
Posts Index
因此:
Blog 页面:
不是:
Page
而是:
文章索引页
七、特殊边界:只设置 Posts Page,不设置 Homepage
例如:
后台:
主页:
未选择
文章页:
Blog
数据库:
show_on_front = page
page_on_front = 0
page_for_posts = 15
访问:
/
很多人认为:
应该是错误配置。
但 WordPress 仍然会:
is_home=true
原因:
源码这里:
if (
$this->is_home
&&
show_on_front === page
&&
page_on_front
)
注意:
这里要求:
page_on_front
必须存在。
但是:
page_on_front = 0
所以不会执行修正。
最终:
is_home=true
is_front_page=false
这也是很多主题开发者没有考虑到的边界情况。
八、首页判断正确方式
很多主题喜欢:
if(is_front_page()){
}
但对于复杂主题不够。
更可靠的模型:
is_home()
|
---------------------
| |
page_for_posts存在 page_for_posts不存在
| |
Blog页面 默认文章首页
is_front_page()
|
静态首页
代码:
if (is_home()) {
$posts_page_id = (int)get_option('page_for_posts');
$current_id = (int)get_queried_object_id();
if (
$posts_page_id > 0 &&
$current_id === $posts_page_id
) {
// Blog 页面
} else {
// 默认文章首页
}
} elseif (is_front_page()) {
// 静态首页
}
九、为什么 SEO Title 尤其需要这样判断?
例如:
SEO 标题:
网站名称 - 首页标题
通常需要支持:
默认首页
读取:
主题设置
home-seo-title
静态首页
读取:
页面 SEO Title
Blog 页面
读取:
Blog 页面 SEO Title
如果简单:
if(is_front_page())
会导致:
默认首页:
is_front_page=true
进入错误逻辑。
十、WordPress 首页判断总结表
| 场景 | is_home | is_front_page | is_page |
|---|---|---|---|
| 最新文章首页 | true | true | false |
| 静态首页 | false | true | true |
| Blog 页面 | true | false | false |
| 只有 Posts Page 首页异常配置 | true | false | false |
十一、主题开发最佳实践
开发主题时:
不要理解:
首页 = is_home()
应该理解:
首页
├── Front Page
│
├── Posts Index
│
└── Posts Page
WordPress 实际有三个概念:
| 概念 | 函数 |
|---|---|
| 网站入口首页 | is_front_page |
| 文章索引 | is_home |
| 普通页面 | is_page |
十二、总结
is_home() 和 is_front_page() 是 WordPress 中最容易误解的一组条件函数。
核心区别:
is_home()
=
文章索引页
is_front_page()
=
网站入口首页
两者可能:
同时 true
也可能:
一个 true,一个 false
最终取决于:
show_on_front
page_on_front
page_for_posts
三个数据库配置。
对于主题开发、SEO 插件开发、首页模块开发,最安全的判断方式:
优先判断 is_home()
再通过 page_for_posts 区分 Blog
最后判断 is_front_page()
处理静态首页
理解 WordPress Query 的真实行为,比简单记忆函数定义更加重要。
这也是开发稳定 WordPress 主题框架时,必须掌握的底层知识。
本文由 好主题 原创整理,致力于分享实用的 WordPress 建站知识与主题开发经验。 我们专注于提供高质量的 WordPress企业主题 资源,帮助中小企业轻松构建专业网站。 转载请注明来源,并保留原文链接,感谢您的支持与理解。