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 trying to load a script from functions.php just when I'm on the front page. I set a static page called "home" in the reading options.

The home page loads the front-page.php template correctly but the conditional script loading doesn't work.

This is what I have in my functions.php file:

wp_register_script('nivoslider', get_bloginfo('template_url').'/js/libs/nivoslider.js', false, false, true);

if (is_front_page()) {   
   wp_enqueue_script('nivoslider'); 
}

Why isn't this loading as expected? What's happening here?

This is my init_scripts function:

function init_scripts() {
    if (!is_admin()) {

        /* Modernizr
         */
        wp_register_script('modernizr', get_bloginfo('template_url').'/js/libs/modernizr.js');

        /* jQuery
        */
        wp_deregister_script('jquery');
        wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false, false, true);

        /* Nivo Slider 
        */
        wp_register_script('nivoslider', get_bloginfo('template_url').'/js/libs/nivoslider.js', false, false, true);

        /* Custom scripts
         */
        wp_register_script('plugins', get_bloginfo('template_url').'/js/plugins.js', false, false, true);
        wp_register_script('script', get_bloginfo('template_url').'/js/script.js',false, false, true);

        wp_enqueue_script('modernizr');       
        wp_enqueue_script('jquery');
        wp_enqueue_script('plugins');
        wp_enqueue_script('script');

        if (is_front_page()) {   
            wp_enqueue_script('nivoslider'); 
        }
    }
}
add_action('init', 'init_scripts');
share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

If you are doing this directly in functions.php you are doing it wrong. It is too early for conditional tags to work.

This should be hooked to wp_enqueue_scripts, see wp_enqueue_script() docs.

share|improve this answer
    
I don't quite understand what I'm doing wrong. I updated the question with my functions.php init_scripts function. All my other scripts load just fine. –  elclanrs Sep 7 '11 at 22:39
    
@elclanrs...your updated code looks good. I'm guessing Rarst thought you were not wrapping the code in a function that was called with a hook. –  tollmanz Sep 8 '11 at 0:02
    
Still doesn't work with the code posted above... –  elclanrs Sep 8 '11 at 4:38
    
@elclanrs as per my answer this needs to be hooked to wp_enqueue_scripts, not init. –  Rarst Sep 8 '11 at 7:21
    
THANK YOU! It works. –  elclanrs Sep 8 '11 at 7:33
add comment

Try using is_home() instead of is_front_page. There is a subtle difference between the two (http://nspeaks.com/1069/difference-between-is_home-and-is_front_page/), which might account for your difficulties.

share|improve this answer
    
Nop. is_home() is for the blog posts home page if I understand correctly. is_front_page() should work but it doesn't. And is_page('Home') doesn't work either... –  elclanrs Sep 8 '11 at 4:39
    
Can you do var_dump(is_front_page()) to see if the variable is correct? You may even want to do global $wp_query; var_dump($wp_query); to further explore if there is something unusual occurring with the query vars. –  tollmanz Sep 8 '11 at 5:18
    
It returns FALSE, wtf! I don't get it! –  elclanrs Sep 8 '11 at 5:28
    
Should i try re-installing WP? –  elclanrs Sep 8 '11 at 5:30
    
No...that's unnecessary. Can you show me what global $wp_query; echo '<pre>'; var_dump($wp_query); echo '</pre>'; prints out? Perhaps do a pastebin or add it to the original question. –  tollmanz Sep 8 '11 at 5:37
show 3 more comments

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.