我有以下代码:
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()
的情况下工作 - 但反之则不然。
始终使用两者,因为它可以更好地控制何时何地使用脚本。
关于javascript - WordPress 队列不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20016615/