Conditionally load scripts and styles
-
Hello
I am looking for a little help with making the following code more efficient. I have this in my theme’s functions.php, but I know it is the long and tedious way of achieving the conditional addition of certain scripts and styles to the head of certain pages. Essentially i have two scripts (each with their own accompanying stylesheets) that I wish to define in a single if(is_page()) statement. Right now there is a lot of duplication that I know can be eliminated with better logic.
Thanks in advance
function rl_load_scripts(){ if (!is_admin()){ wp_deregister_script('jquery'); wp_register_script('jquery', ("https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"), false); wp_register_script('nivo_slider', (get_bloginfo('template_directory') . '/scripts/nivo.slider/jquery.nivo.slider.pack.js'), array('jquery')); wp_register_script( 'slidingBoxes', (get_bloginfo('template_directory') . '/scripts/slidingBoxes/slidingBoxes.js'), array('jquery')); // jQuery wp_enqueue_script('jquery'); // Nivo Slider if(is_page('home')){ wp_enqueue_script('nivo_slider'); } // Sliding Boxes if(is_page('test')){ wp_enqueue_script( 'slidingBoxes'); } } } add_action('wp_print_scripts','rl_load_scripts'); function rl_load_styles(){ if (!is_admin()){ wp_register_style('nivo_slider', (get_bloginfo('template_directory') . '/scripts/nivo.slider/rl-nivo-slider.css')); wp_register_style('slidingBoxes', (get_bloginfo('template_directory') . '/scripts/slidingBoxes/slidingBoxes.css')); // Nivo Slider if(is_page('home')){ wp_enqueue_style('nivo_slider'); } // Sliding Boxes if(is_page('test')){ wp_enqueue_style('slidingBoxes'); } } } add_action( 'wp_print_styles', 'rl_load_styles' );
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Conditionally load scripts and styles’ is closed to new replies.