为了账号安全,请及时绑定邮箱和手机立即绑定

在 childtheme wordpress 中出列和入队缩小的 js 文件

在 childtheme wordpress 中出列和入队缩小的 js 文件

FFIVE 2021-08-20 14:33:30
我正在尝试从 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);代码中是否有我遗漏的东西,或者是否有一种特殊的方式使缩小的脚本出队/入队?任何帮助将不胜感激!:)
查看完整描述

1 回答

?
翻翻过去那场雪

TA贡献2065条经验 获得超13个赞

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


使用此代码:


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 变量,因为它不是必需的。


查看完整回答
反对 回复 2021-08-20
  • 1 回答
  • 0 关注
  • 115 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信