如何使此WP script_loader_tag工作



我在这里读了大约20多篇文章,但不明白为什么这不起作用。。。

我将两个脚本排队,然后使用script_loader_tag添加type="模块";每个标签。脚本被加载,但没有类型=";模块";出现。

functions.php代码如下,网站为radhr.org,非常感谢您的帮助。。。

// Load RoughNotation page title animations
function notation_page_js() {
if( !is_page('2') ) {
wp_enqueue_script( 'js-file', get_stylesheet_directory_uri() . '/js/rn-page.js');
}
}
add_action('wp_enqueue_scripts', 'notation_page_js');
// Load RoughNotation home hero animation
function notation_home_js() {
if( is_page('2') ) {
wp_enqueue_script( 'js-file', get_stylesheet_directory_uri() . '/js/rn-home.js');
}
}
add_action('wp_enqueue_scripts', 'notation_home_js');
// Add module type to RoughNotation script tags
function add_type_attribute($tag, $handle, $src) {
// if not your script, do nothing and return original $tag
if ( 'notation_page_js || notation_home_js' === $handle ) {
// change the script tag by adding type="module" and return it.
$tag = '<script type="module" src="'. esc_url($src) .'"></script>';
}
return $tag;
}
add_filter('script_loader_tag', 'add_type_attribute' , 10, 3);

我认为您应该更改

if ('notation_page_js || notation_home_js' === $handle)

通过

if ('js-file' === $handle)

最新更新