我需要在我的主题中添加旋转木马滑块。它需要jQuery,而jQuery是在WP核心中构建的。如何使用此添加旋转木马脚本?
如何将带有jQuery的插件添加到自定义主题中?
1 个回复
SO网友:Zeth
应该说,WordPress附带了现成的jQuery。你可以检查它,如果你在你的页面模板中添加这个(based on this post):
window.onload = function() {
if (window.jQuery) {
// jQuery is loaded
alert("Yeah!");
} else {
// jQuery is not loaded
alert("Doesn\'t Work");
}
}
如果您需要其他脚本,那么可以查看wp_enqueue_script-作用<小时>Only load script on certain pages
根据您的评论,我想我添加了一个片段,您可以尝试(从this page):
function custom_enqueue_scripts() {
if ( is_page_template( \'page-template-name.php\' ) ) {
wp_enqueue_script( \'custom-script\', get_stylesheet_directory_uri() . \'/assets/js/YOUR_CUSTOM_SCRIPT.js\', array( \'jquery\' ) );
}
}
add_action( \'wp_enqueue_scripts\', \'custom_enqueue_scripts\' );
如您所见,第三个参数是array( jquery )
, 这是一组依赖项。这意味着在加载给定脚本(jquery)之前不会加载脚本。超级智能!:-)<小时>Load script on all pages
这是您希望在所有页面上加载脚本的情况。
function custom_enqueue_scripts() {
wp_enqueue_script( \'custom-script\', get_stylesheet_directory_uri() . \'/assets/js/YOUR_CUSTOM_SCRIPT.js\', array( \'jquery\' ) );
}
add_action( \'wp_enqueue_scripts\', \'custom_enqueue_scripts\' );
结束