adding conditionals to load script
-
I have added conditional tags to javascript loading function
if (!is_admin()) add_action( 'wp_print_scripts', 'theme_javascript_home' ); function theme_javascript_home( ) { wp_reset_query(); if (is_home()) { wp_enqueue_script( 'script1', get_bloginfo('template_directory').'/js/script1.js', array( 'jquery' ) ); } } if (!is_admin()) add_action( 'wp_print_scripts', 'theme_javascript_single' ); function theme_javascript_single( ) { if (is_single()) { wp_enqueue_script( 'script2', get_bloginfo('template_directory').'/js/script2.js'); } } remove_action('wp_head', 'wp_print_scripts'); add_action('wp_footer', 'wp_print_scripts', 5);Why I did it? So script1 (home page) will be loaded only when browsing home page. As many visitors get to the single page first and it takes a few clicks untill they get to the home page. It doesn’t load jquery, which I use on the home page only, untill visitor gets to the home page.
Firebug shows difference.
Would someone comment on this practice. Is it good ?
Also you can see that using function loads javascript in the footer.
Thank you
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
The topic ‘adding conditionals to load script’ is closed to new replies.