Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Wordpress wp_排队_脚本不工作_Javascript_Wordpress - Fatal编程技术网

Javascript Wordpress wp_排队_脚本不工作

Javascript Wordpress wp_排队_脚本不工作,javascript,wordpress,Javascript,Wordpress,我正在开发一个主题,并试图让wp_enqueue_脚本正常工作。奇怪的是,什么也没出现。它没有任何作用。以下是我的设置: 在functions.php中,我有: function named_scripts() { global $named_options; if( is_admin() ) return; wp_deregister_script( 'jquery' ); wp_register_script( 'screen', tz_JS . '/s

我正在开发一个主题,并试图让wp_enqueue_脚本正常工作。奇怪的是,什么也没出现。它没有任何作用。以下是我的设置:

在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' );
在header.php中,我调用

named_scripts();
在HTML中,没有显示任何内容。

常量“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\u enqueue\u脚本
是用于加载前端脚本()的合适挂钩。您不需要检查
is_admin()
,因为
admin_enqueue_scripts
是在管理端加载脚本的相应钩子

杰拉尔德很在行。您已取消注册Wordpress附带的jQuery,但未注册替代版本

如果您想直接从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');

如果您想取消注册,您需要立即注册另一个版本,然后根据jQuery授予其他JS

您应该在删除默认的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');

如果您正在开发子主题在主题目录中加载js时,请使用
get\u stylesheet\u directory\u uri()

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' );

在将jquery用作依赖项之前取消注册(=删除)有什么特别的原因吗?在我开始之前,请注意:
add_action('wp_head',…)而不是od
init
,确保
wp_head()header.php
中调用code>,调用
命名的_脚本()功能应为nessecary。