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.

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 !!!

share|improve this question
    
the is_home part of the code is inside some function which is hooked to wp_enqueue_scripts action? –  Hameedullah Khan Sep 5 '11 at 10:33
add comment

1 Answer 1

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.

share|improve this answer
    
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 Sep 5 '11 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 Sep 5 '11 at 11:45
    
@Rarst Then where do you do that outside of functions.php? –  Little Big Bot Jan 4 '13 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 Jan 4 '13 at 19:50
add comment

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.