Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In the Shoestrap theme, you often change content by using hooks. I want to add the following to functions.php but it doesn't work...

// in functions.php
if ( is_home() ) {
  add_action( 'shoestrap_below_top_navbar', 'my_custom_below_top_navbar' );
}
// also tried since we're before the loop
if ( 684 == get_queried_object_id() ) {
  add_action( 'shoestrap_below_top_navbar', 'my_custom_below_top_navbar' );
}
// but get_queried_object_id() returns 0

I'm using WP 3.8. I found this bug report but it doesn't seem to be quite the same issue since my page is just the home page, not a query page.

ps, if someone would like to create a shoestrap tag, please add it to this q.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

I'm pretty sure you try to use this conditional tags to early, so they don't work.

From Codex:

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.

So what should you do?

Just put your code in some action, that is run later.

function my_wp_action_callback() {
    if ( is_home() ) {
        add_action( 'shoestrap_below_top_navbar', 'my_custom_below_top_navbar' );
    }
}
add_action( 'wp', 'my_wp_action_callback' );

The other way to do this would be to add actions always and put conditional statements inside them:

function my_custom_below_top_navbar() {
    if ( !is_home() )
        return;
    // ... 
}
add_action( 'shoestrap_below_top_navbar', 'my_custom_below_top_navbar' );
share|improve this answer
    
Thanks, the wp action seems exactly right for my purposes. –  Larry K Dec 16 '13 at 12:34
    
I tried your solution but is_home() still did not work in the wp action. But get_queried_object_id() did work so that will be ok for now. –  Larry K Dec 16 '13 at 12:41
    
Hmm, it should work. Do you have static front page? If so, you should use is_front_page() instead of is_home(). –  Krzysiek Dróżdż Dec 16 '13 at 12:43
    
Yes, that was the problem! Thank you for your great help, much appreciated. –  Larry K Dec 16 '13 at 12:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.