Вопрос

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

Нет правильного решения

Лицензировано под: CC-BY-SA с атрибуция
Не связан с wordpress.stackexchange
scroll top