javascript Wordpress wp_enqueue_script 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14606823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:04:53  来源:igfitidea点击:

Wordpress wp_enqueue_script Not Working

javascriptwordpress

提问by William H

I'm developing a theme and trying to get wp_enqueue_script to work. The curious thing, is that nothing shows up. It doesn't do anything. Here's my setup:

我正在开发一个主题并试图让 wp_enqueue_script 工作。奇怪的是,什么都没有出现。它什么也不做。这是我的设置:

in functions.php I have:

在functions.php中我有:

function named_scripts() {

    global $named_options;

    if( is_admin() ) return;

    wp_deregister_script( 'jquery' );
    wp_register_script( 'screen', tz_JS . '/screen.js', array( 'jquery' ) );
    wp_enqueue_script( 'screen' );
    wp_enqueue_script( 'bootstrap', tz_JS . '/bootstrap/bootstrap.js', array( 'jquery' ) );

    wp_register_style( 'custom-style', get_template_directory_uri() . '/css/custom-style.css', array(), '20120208', 'all' );  
    wp_enqueue_style( 'custom-style' );

}



add_action( 'init', 'named_scripts' );

in header.php I call

在 header.php 我打电话

named_scripts();

And in the HTML, nothing shows up at all.

在 HTML 中,什么也没有显示。

回答by WillxD

You should have registered your jquery file after remove the default wordpress jquery. I use this code.. hope it helps..

您应该在删除默认的 wordpress jquery 后注册您的 jquery 文件。我使用此代码..希望它有帮助..

function load_external_jQuery() { // load external file  
    wp_deregister_script( 'jquery' ); // deregisters the default WordPress jQuery  
    wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"), false);
    wp_enqueue_script('jquery');
    wp_register_script('blur', get_template_directory_uri() . '/_/js/blur.js', array('jquery') );
    wp_enqueue_script('blur');
}  
add_action('wp_enqueue_scripts', 'load_external_jQuery');

回答by McNab

Gerald is spot on. You've deregistered the jQuery that comes with Wordpress without registering an alternative version.

杰拉德就位。您已经注销了 Wordpress 附带的 jQuery,而没有注册替代版本。

Normally the shipped version is removed if you want to load it directly from a CDN. An example would be below;

如果您想直接从 CDN 加载它,通常会删除附带的版本。下面是一个例子;

  wp_deregister_script('jquery');
  wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', false, '1.7.2');
  wp_enqueue_script('jquery');

If you want to deregister it you need to register another version straight away before enqueing other JS dependant on jQuery

如果你想注销它,你需要在排队其他依赖于 jQuery 的 JS 之前立即注册另一个版本

回答by helgatheviking

Is the constant "tz_JS" is defined correctly? Presuming yes, you should be able to simplify your function like so:

常量“tz_JS”是否定义正确?假设是的,您应该能够像这样简化您的功能:

function named_scripts() {

    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'screen', tz_JS . '/screen.js', array( 'jquery' ) );
    wp_enqueue_script( 'bootstrap', tz_JS . '/bootstrap/bootstrap.js', array( 'jquery' ) );

    wp_enqueue_style( 'custom-style', get_template_directory_uri() . '/css/custom-style.css', array(), '20120208', 'all' );  

}
add_action( 'wp_enqueue_scripts', 'named_scripts' );

wp_enqueue_scriptsis the proper hook to use for loading front-end scripts (see Codex). You don't need to check is_admin()since admin_enqueue_scriptsis the corresponding hook for loading scripts on the admin side.

wp_enqueue_scripts是用于加载前端脚本的正确钩子(参见 Codex)。您不需要检查,is_admin()因为admin_enqueue_scripts是在管理端加载脚本的相应挂钩。

回答by adedoy

If you are developing a child themeuse get_stylesheet_directory_uri()when loading js within your theme directory.

如果您正在开发子主题get_stylesheet_directory_uri(),请在主题目录中加载 js 时使用。

function named_scripts() {
  wp_enqueue_script('jquery');
  wp_enqueue_script(
        'default_scripts', 
        get_stylesheet_directory_uri() . '/js/scripts.js', 
        array('jquery'),
        '1.0',
        false
        );
}
add_action( 'wp_enqueue_scripts', 'named_scripts' );