Created
September 28, 2014 16:45
-
-
Save gabrieleromanato/ac782d48b59d2f22502a to your computer and use it in GitHub Desktop.
WordPress: conditional script inclusion
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 | |
class MyJS { | |
public function __construct() { | |
add_action( 'wp_enqueue_scripts', array( &$this, 'addScripts' ) ); | |
} | |
public function addScripts() { | |
// jQuery | |
wp_deregister_script( 'jquery' ); | |
wp_register_script( 'jquery', get_template_directory_uri() . '/js/jquery.js', array(), '2.2', true ); | |
wp_enqueue_script( 'jquery' ); | |
if( is_single() ) { // singolo post? | |
global $post; // oggetto globale $post | |
if( has_tag( 'galleria', $post ) ) { // il post ha il tag 'galleria' ? | |
wp_register_script( 'fancybox', get_template_directory_uri() . '/js/plugins/fancybox/jquery.fancybox.js', array( 'jquery' ), '2.0', true ); | |
wp_enqueue_script( 'fancybox' ); | |
wp_register_style( 'fancybox', get_template_directory_uri() . '/js/plugins/fancybox/jquery.fancybox.css' ); | |
wp_enqueue_style( 'fancybox' ); | |
} | |
} | |
// script principale | |
wp_register_script( 'blog', get_template_directory_uri() . '/js/blog.js', array( 'jquery' ), '1.0', true ); | |
wp_enqueue_script( 'blog' ); | |
} | |
} | |
$myjs = new MyJS(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment