Created
July 2, 2018 20:18
-
-
Save zachary-russell/77e9fa61c772b40c2fb934e38369991a to your computer and use it in GitHub Desktop.
Conditionally Enqueue JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Hook into WordPress to load the scripts | |
add_action('wp_enqueue_scripts', 'mfp_conditional_scripts'); | |
function mfp_conditional_scripts() { | |
// Add global script | |
wp_enqueue_script('global', get_stylesheet_directory_uri() . '/js/global.min.js', ['jquery'], '', true); | |
if( is_home() ) { | |
// Homepage Scripts | |
wp_enqueue_script('homepage', get_stylesheet_directory_uri() . '/js/homepage.min.js', ['jquery'], '', true); | |
} | |
else if( is_single()) { | |
// Blog post scripts | |
wp_enqueue_script('blog-post', get_stylesheet_directory_uri() . '/js/blog-post.min.js', ['jquery'], '', true); | |
} | |
else if( is_tax()) { | |
// Taxonomy scripts - i.e. Categories & Tags | |
wp_enqueue_script('taxonomy', get_stylesheet_directory_uri() . '/js/taxonomy.min.js', ['jquery'], '', true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment