0

I'm having the following issue:
some conditional tags do not work on the functions.php file.

For example I'm trying to do the following:

if ( is_home() ) :
    wp_enqueue_script('accordeon_setup', '/wp-content/themes/44db/js/scripts-accordeon.js');
endif;

and although the site produces no error I cannot see the script in the home page.
The same thing happens with the following conditional tag:

if ( $post->post_parent == '12' ) :

Is there something I'm missing?? Both conditional tags run at the bottom of the functions.php

Thank you people !!!

1
  • the is_home part of the code is inside some function which is hooked to wp_enqueue_scripts action? Commented Sep 5, 2011 at 10:33

1 Answer 1

2

functions.php is part of the loading process and way too early for conditional tags to work reliably. Any conditional logic should be hooked to and executed no earlier than template_redirect hook.

For scripts on front-end wp_enqueue_scripts is appropriate hook.

4
  • great, thanks for the prompt respond. So how do you do it? I want to load specific javascripts according to the page I'm in.
    – ion
    Commented Sep 5, 2011 at 11:41
  • 1
    @ion wp_enqueue_script() docs in Codex have plenty of examples of hooking. You only need to add conditional inside hooked function.
    – Rarst
    Commented Sep 5, 2011 at 11:45
  • @Rarst Then where do you do that outside of functions.php?
    – Belladonna
    Commented Jan 4, 2013 at 17:59
  • @Little Big Bot you are confusing where code is located and when code is executed. Code can (and usually plenty of it is) be located inside functions.php, but be hooked to execute at point different from when functions.php file itself is loaded. Basically this is difference between where function is defined and where it is called.
    – Rarst
    Commented Jan 4, 2013 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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