WordPress 排队不起作用

Wordpress enqueue not working

本文关键字:不起作用 排队 WordPress      更新时间:2023-09-26

我有以下代码:

function register_scripts(){
    wp_register_style( 'new_style', plugins_url('/css/style.css', __FILE__));
    wp_register_script( 'google-maps-api', 'http://maps.google.com/maps/api/js?sensor=false' );
}
add_action('wp_enqueue_scripts', 'register_scripts'); 

但是它不起作用,谁能看到我做错了什么?

喜欢在评论中 - 您已经注册了它们,但没有排队...

function regiqueue_scripts(){
    wp_register_style( 'new_style', plugins_url('/css/style.css', __FILE__));
    wp_register_script( 'google-maps-api', 'http://maps.google.com/maps/api/js?sensor=false' );
    wp_enqueue_style( 'new_style' ); // or use just enqueue without register .. but not the other way around 
    wp_enqueue_script( 'google-maps-api' ); 
}
add_action('wp_enqueue_scripts', 'regiqueue_scripts'); 

您会看到 - 注册脚本只是使它们可供使用,但在您告诉它这样做之前,它不会排队。 函数wp_enqueue_xx() - 当所有参数填充时都可以在没有wp_register_xx()的情况下工作 - 但反之则不然。

始终同时使用两者,因为它可以更好地控制何时何地使用脚本。