1

我正在尝试从 wordpress 父主题中取出一个缩小的 js 文件。似乎无法弄清楚如何将我自己的自定义 js 文件从子主题中出列然后入队。

这是我的代码:

    add_action( 'wp_enqueue_scripts', 'my_remove_scripts', 20 );

    function my_remove_scripts(){
    wp_dequeue_script( 'custom' );
    }
    add_action('wp_enqueue_scripts', 'my_add_scripts');

    function my_add_scripts(){

    wp_enqueue_script('custom-child', get_template_directory_uri() . '/js/custom'.$js_minify_prefix.'.js', array('jquery'), THEME_VERSION, true);
    }

父主题中的脚本是这样排队的:

    wp_enqueue_script('custom', get_template_directory_uri() . '/js/custom'.$js_minify_prefix.'.js', array('jquery'), THEME_VERSION, true);

代码中是否缺少某些内容,或者是否有一种特殊的出列/入列缩小脚本的方法?任何帮助,将不胜感激!!:)

4

1 回答 1

3

您的代码中只有一个错误。您正在使同一个文件出队,然后将其入队。

使用此代码:

add_action( 'wp_enqueue_scripts', 'my_remove_scripts', 20 );

function my_remove_scripts(){
wp_dequeue_script( 'custom' );
}
add_action('wp_enqueue_scripts', 'my_add_scripts');

function my_add_scripts(){

wp_enqueue_script('custom-child', get_stylesheet_directory_uri() . '/js/custom.min.js', array('jquery'), 1.0, true);
}

get_stylesheet_directory_uri()是这里的关键。它返回子主题的路径。另外,我已经从代码中删除了 $js_minify_prefix 变量,因为它不是必需的。

于 2019-07-24T08:06:34.717 回答