• Resolved jeffinmotion

    (@jeffinmotion)


    Hi,

    I’m creating a theme based on twentyten, and I’ve got a function called custom_head in functions.php that I’m using to register and enqueue scripts. I run custom_head by adding it to my init action:

    add_action('init', 'custom_head');

    In my custom_head function, is_home() does not work. If I use is_home() in my index.php file, I get the correct expected value (true).

    I did a little digging and found that all is_home() does is:

    global $wp_query;
    return $wp_query->is_home;

    So I did a print_r on the global $wp_query in my custom_head function and found an empty object. Any ideas why this is? Is there a more proper action I should be using to register my scripts where the global $wp_query object would be populated and valid?

Viewing 5 replies - 1 through 5 (of 5 total)
  • You are calling the function on init before any page is loaded?

    The way you would do this is just to create the function in the child themes function.php without the add_action()

    Copy header.php to the child theme and then add your call to your function in header.php like.

    <?php custom_head(); ?>

    If this does not answer your question then a bit more detail about the custom_head functions job would help!

    HTH

    David

    I just read your post again, and you are loading scripts, it reads like you saying that these are conditional, so they only load if the home page is called?

    Then you would call the function from header.php, as only then will the page id be known to WordPress.

    HTH

    David

    Thread Starter jeffinmotion

    (@jeffinmotion)

    Hi David,

    Yes, you surmise correctly that I am trying to conditionally include a script on the homepage — and the solution you describe is exactly how I initially worked around this issue! I appreciate your response.

    However, I really like the mechanism that WordPress has for queueing/dequeuing scripts with support for dependencies. It’s a really clean way to declare my scripts and I’d love to use it — so I poked around the action reference and answered my own question.

    The answer is: is_home() will work the way I want it to if I register my function under the get_header action. Like:

    add_action('get_header', 'custom_head');

    Thanks!

    So much hope! Crushed into dust… ??

    This didn’t work for me. Here’s my scenario:

    Instead of using functions.php for custom functions, I add them to a plug-in. So, inside the PHP for the plugin I have soemething like:

    add_action('get_header', 'my_method');
    function my_method() {
      if (is_home()) { //have also used is_page('home') to no avail
        ..do stuff..
      }
    }

    The “stuff” in question is bit of enqueuing and localizing scripts as well as supplying a function for processing AJAX calls. I will fall back to calling it from the header, but I’d rather not, mainly because the client hasn’t settled on a theme and by moving the custom functions plugin, I can have custom development be more instantly portable (vs. editing the header each time).

    Any advice would be awesome. Even a totally different way to enable WP Conditionals for use inside a neo-functions.php file.

    Ew my gawd.

    I had a typo in my function. That’s all it was.

    D’oh!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘is_home() not working in functions.php’ is closed to new replies.