最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

actions - What is the earliest possible hook for safely using `is_front_page`?

programmeradmin0浏览0评论

When a page request is made, at what point does is_front_page start working? As I recall, if you try using it too early it will always return false.

My assumption is that the wp action is the first point that is_front_page will work, however, the conditional check for is_front_page is part of the WP_Query class, so wouldn't parse_query then be the earliest safe use?

When a page request is made, at what point does is_front_page start working? As I recall, if you try using it too early it will always return false.

My assumption is that the wp action is the first point that is_front_page will work, however, the conditional check for is_front_page is part of the WP_Query class, so wouldn't parse_query then be the earliest safe use?

Share Improve this question asked Apr 20, 2019 at 22:21 JRad the BadJRad the Bad 1684 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 7

The parse_query() method of the WP_Query class sets the variables on which the conditional tags are based.
The parse_query hook are executed before returning from parse_query() method, variables are already set (eg. is_home, used by is_front_page()) and is_front_page() will work, but function that changes the current state can be hooked to parse_query (it's still parse_query() method, which sets variables like is_home).

Hooks pre_get_posts is executed directly after parse_query, that is why it is for me the first safe hook to use conditional tags.

Conditional Tags in Codex

You can only use conditional query tags after the posts_selection action hook in WordPress (the wp action hook is the first one through which you can use these conditionals). For themes, this means the conditional tag will never work properly if you are using it in the body of functions.php, i.e. outside of a function.

However: if you have a reference to the query object (for example, from within the parse_query or pre_get_posts hooks), you can use the WP_Query conditional methods (eg: $query->is_search())

发布评论

评论列表(0)

  1. 暂无评论