有趣的新方法,注册和排队您的主题脚本。还可以在需要时更轻松地添加和删除新脚本。
/**
* Registers javascript using wp_register_script
*
* \'$handle\' => array(
* \'remote\' => boolean (true|false), //optional default false
* \'src\' => \'file_name.js\', //string required
* \'deps\' => \'dependencies\', //string optional
* \'footer\' => boolean (true|false) //optional default false
*/
function scripts() {
$deregister = array(
\'jquery\' => array()
);
$scripts = array(
\'jquery\' => array(
\'remote\' => true,
\'src\' => \'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js\',
\'deps\' => null,
),
\'jquery.cycle\' => array(
\'remote\' => false,
\'src\' => \'jquery-cycle/jquery.cycle.min.js\',
\'deps\' => \'jquery\'
),
\'jquery.jcarousel\' => array(
\'remote\' => false,
\'src\' => \'jquery.jcarousel.min.js\',
\'deps\' => \'jquery\'
),
\'functions\' => array(
\'src\' => \'functions.js\',
\'deps\' => \'jquery\',
\'footer\' => true,
)
);
foreach ( $deregister as $key => $handle ) :
if ( ! is_admin() ) :
wp_deregister_script( $key );
endif;
endforeach;
foreach ( $scripts as $key => $value ) :
if ( is_admin() ) return;
if ( is_array( $value ) && array_key_exists( \'src\', $value ) ) :
$src = !empty( $value[ \'remote\' ] ) ? $value[ \'src\' ] : THEME_JS_URI . $value[ \'src\' ];
if ( ! $value[\'deps\'] == null ) :
wp_register_script( $key, $src, $value[ \'deps\' ], false, !empty( $value[ \'footer\' ] ) );
else : wp_register_script( $key, $src, false, !empty( $value[ \'footer\' ] ) );
endif;
else : wp_register_script( $key, THEME_JS_URI . $src, $value, false );
endif;
wp_enqueue_script( $key );
endforeach;
}