Get current page_id before loop, in functions.php

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.

Read More

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

Related posts

1 comment

  1. 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' );
    

Comments are closed.