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

frontpage - is_front_page only works in theme file, and does not work in functions.php

programmeradmin0浏览0评论

If I use is_front_page() in my front-page.php file it works as expected. However if I use the same function in my theme functions.php file it does not work. Is this normal behavior? If not can it be trouble shot somehow?

In settings->reading I have my front page set to a page called "front page" so I think that is all set.

EDIT

adding the following to functions.php does not work, however I promise it does work as expected if I add is_home_page() to my header.php file

add_action('init', 'my_test');

function my_test(){
    if(is_front_page())
        echo 'is it?';
}

If I use is_front_page() in my front-page.php file it works as expected. However if I use the same function in my theme functions.php file it does not work. Is this normal behavior? If not can it be trouble shot somehow?

In settings->reading I have my front page set to a page called "front page" so I think that is all set.

EDIT

adding the following to functions.php does not work, however I promise it does work as expected if I add is_home_page() to my header.php file

add_action('init', 'my_test');

function my_test(){
    if(is_front_page())
        echo 'is it?';
}
Share Improve this question edited Feb 9, 2012 at 19:55 Mike asked Feb 9, 2012 at 19:36 MikeMike 8234 gold badges13 silver badges21 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 6

That may be normal behavior, depending on how exactly you're using is_front_page in your functions file.

If you use it inside the global scope of functions.php, it will not work.

Why? Because WordPress loads functions.php before the $wp_query object has been set up with the current page. is_front_page is a wrapper around around $wp_query->is_front_page(), and if the query hasn't been set up, it's always going to return false (or throw a warning, if you have wp_debug on.

From the codex:

Warning: You can only use conditional query tags after the init action hook in WordPress. 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.

http://codex.wordpress/Conditional_Tags

The codex quote by chrisguitarguy is no longer valid!! It has been updated to:

Warning: 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.

Thus, the init hook no longer works!!! As stated, the wp hook is the first hook you can use if you want to use conditional query tags. So, to make conditional tags work, here is the minimal boilerplate:

add_action('wp', 'function_that_uses_conditional_tags');

function function_that_uses_conditional_tags() {
    // place code here
}

However, please note that while that "warning" seems universally applicable to conditional tags as it appears in the very introduction to the codex page about Conditional Tags, it fails to emphasize that it really only applies to the frontend.

The wp hook does not even fire in the backend! So if you use it for instance with is_admin(), it will never evaluate to true. Use a different hook in that case. Actually, init works just fine for is_admin() ;).

This is expected behavior. The functions.php file is parsed before the query is setup and available, so if you have if ( is_front_page() ) sitting naked inside of functions.php, it will return false, because there's no query yet.

What you need to do is to put your is_front_page() conditional inside of a callback function, that is then hooked into an appropriate action - i.e. an action that fires after the query is setup/available.

For reference, I think functions.php is parsed at plugins_loaded (it might be setup_theme), and the query conditionals should be available at or after init.

Edit

plugins_loaded and setup_theme do not work for me

Of course those hooks won't work for you. They fire before the query is setup. The is_front_page() conditional is only available after the query is setup, which happens at init.

I need to remove_action() if the user is viewing the front page.

You're not really telling us exactly what you're trying to do. Your question simply asked when is_front_page() available, which is what we answered. Knowing how/when to remove an action is a mostly entirely different question. You need to provide the add_action() call you're wanting to remove.

The earlier hooks that template_tags are working is wp action hook. All hooks before that one like init hook won't work for template tags like is_front_page()

发布评论

评论列表(0)

  1. 暂无评论