我试图从子主题加载init.js
,并从父主题卸载该主题。
其结果是,我现在从两个文件中看到页面样式。为什么父文件未被卸载,我如何才能卸载它?
我在/child-theme/function.php
中添加了这个
add_action( 'wp_enqueue_scripts', 'wpshout_dequeue_and_then_enqueue', 100 );
function wpshout_dequeue_and_then_enqueue() {
// Dequeue (remove) parent theme script
wp_dequeue_script( '/js/init.js' );
// Enqueue replacement child theme script
wp_enqueue_script(
'modified_child_script',
get_stylesheet_directory_uri() . '/js/init.js',
array( 'jquery' )
);
}
这两个文件具有相同的名称,并且都在
parent-theme
-js/init.js
和
child-theme
-js/init.js
发布于 2018-09-18 11:08:55
这里有几个问题要从开始
解决方案:
将父js文件复制到您的子主题,并打开它并进行必要的更改。保存文件
现在,您需要将父js文件排出队列并取消注册,然后将您的新子主题js文件加入队列。
add_action( 'wp_enqueue_scripts', 'wpshout_dequeue_and_then_enqueue', 100 );
function wpshout_dequeue_and_then_enqueue() {
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() . '/js/init.js' );
//Now we have done it correctly
}
谢谢!
https://stackoverflow.com/questions/52384395
复制相似问题