admin管理员组文章数量:1125929
I've found that is_front_page
appears to return true when I'm viewing the home page and have a single sticky post assigned there.
It also returns true when I've assigned a page as the static front page via Settings > Reading.
Why would I ever want to use is_home()
?
I've found that is_front_page
appears to return true when I'm viewing the home page and have a single sticky post assigned there.
It also returns true when I've assigned a page as the static front page via Settings > Reading.
Why would I ever want to use is_home()
?
4 Answers
Reset to default 80is_front_page()
returns true if the user is on the page or page of posts that is set to the front page on Settings->Reading->Your homepage displays
So if you set about us
as the front page then this conditional will only be true if showing the about us page.
is_home()
return true when on the posts list page, This is usually the page that shows the latest 10 posts.
If the settings under Your homepage displays are left at default then the home page will return true for both is_front_page()
and is_home()
An example of using is_home()
:
- You have set your posts page to a page called News.
- A user navigates there and in the header you want to show additional navigation
- You could use
is_home()
to do this.
I've discovered that is_home()
and is_front_page()
don't deliver what's expected for multisites. My workaround using built in PHP goodies:
if($_SERVER['REQUEST_URI'] == '/') {
// you must be on the home page
}
As mentioned in the comments, this approach will not work for WP instances installed in subdirectories of the web root. Use at your discretion.
You'd want to use is_home()
when you want to check if the user is viewing your list of blog posts (usually set to display 10 posts per page). If you have a home.php
file in your theme, that will be displayed when the is_home()
condition is true.
The following can possibly remove some confusion as well: when is_front_page()
and is_home()
conditions, both are true, the template front-page.php
will be used instead of home.php
.
- You visit a frontpage that is a blog homepage => is_front_page() = TRUE && is_home() = TRUE
- You visit a front page that is a static page => is_front_page() = TRUE && is_home() = FALSE
- You visit the blog homepage but your front page is a static page => is_front_page() = FALSE && is_home() = TRUE (Also TRUE when reaching paginated pages)
References for definitions:
https://developer.wordpress.org/reference/functions/is_home/ https://developer.wordpress.org/reference/functions/is_front_page/
本文标签:
版权声明:本文标题:theme development - When to use is_home() vs is_front_page()? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736663744a1946552.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论