我有一个子主题,我可以添加我想用来替换一些主题功能并替换一些按钮的脚本。
但我无法删除旧按钮,因此它们都显示在彼此之上。如何删除父js脚本?
这是我的function.php
为儿童主题
function replace_scroll(){
// Remove the Default JavaScript
wp_dequeue_script( 'dp-js' );
// Add your own script
$js_url = get_bloginfo('stylesheet_directory') . '/js';
wp_enqueue_script('dp',"$js_url/dp1.scripts.js");
}
add_action( 'wp_enqueue_scripts', 'replace_scroll' );
首先要解决几个问题
你应该使用get_stylesheet_directory_uri()
对于儿童主题和get_template_directory_uri()
对于父主题而不是get_bloginfo()
功能。后者较慢并且使用前两个函数
脚本和样式应该被注销AND并出队以将它们从队列中完全删除
优先级很重要。您需要稍后挂钩您的函数,以确保在取消注册样式和脚本之前已注册它们,否则它将无法工作。
解决方案:
将父 js 文件复制到您的子主题并打开它并进行必要的更改。保存文件
现在你需要出队AND取消注册父 js 文件,然后将新的子主题 js 文件加入队列
Example
/*
* Use any number above 10 for priority as the default is 10
* any number after 10 will load after
*/
add_action( 'wp_enqueue_scripts', 'my_custom_scripts', 100 );
function my_custom_scripts()
{
wp_dequeue_script( 'parent-script-handle' );
wp_deregister_script( 'parent-script-handle' );
// Now the parent script is completely removed
/*
* Now enqueue you child js file, no need to register if you are not
* doing conditional loading
*/
wp_enqueue_script( 'child-script-handle', get_stylesheet_directory_uri() . '/child-script.js' );
//Now we have done it correctly
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)