javascript - WordPress 队列不工作

标签 javascript php wordpress

我有以下代码:

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/

相关文章:

javascript - 使用javascript同步两个div

javascript - 如何使用 javascript/jquery 仅显示具有特定类的 li?

php - 如何使用 php 在 td 中发送 contenteditable 值

wordpress - 如何防止 WordPress 从 URL 查询字符串中剥离 "at"符号 (@)?

php - 如何更改购物车页面上的自定义费用顺序(升序/降序)(woocommerce)

wordpress - 如何在表格中显示 WordPress 自定义页面的 the_meta()

javascript - 关于数组和对象的基本问题

javascript - 使用 jQuery 可排序(不是表单)的 AJAX 发布附加数据

php - 如何禁用 fsockopen 中的 ssl 检查? (PHP 5.6)

php - 获取 Magento 类别 URL 和名称