2

我正在尝试使用以下方法加载一个名为accordion.js 的脚本:

function accordion() {
    wp_register_script( 
        'accordion',  
        get_template_directory_uri() . '/js/accordion.js',
        array('jquery'),
        null, 
        true 
        );
    wp_enqueue_script('accordion');
}

它没有加载脚本(因为它没有出现在带有 Chrome devtools 的资源中),我不知道我做错了什么。我意识到这可能是非常基本的,但我没有尝试解决它。

4

1 回答 1

0

The snippet you've included will not work on its own (if you've dropped that into your functions.php file). You'll also need to ensure that you call your function with the proper hook like so:

add_action('wp_enqueue_scripts', 'accordion');

This will tell WordPress to run the accordion function when it's time to enqueue the scripts.

Additionally, you might want to rename your function to something more generic such as load_resources and you can enqueue all of your scripts that you'll need inside that function.

If you're doing that successfully, but you still have problems, you might check the Net tab of your developer tool and see if the browser is at least attempting to load the file (if even from the wrong location).

于 2013-01-03T19:10:00.617 回答