【问题标题】:Wordpress enqueue not workingWordpress 排队不工作
【发布时间】:2013-11-16 08:58:29
【问题描述】:

我有以下代码:

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

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

【问题讨论】:

  • 你确定,add_action 被调用了吗?
  • Philipp,感谢您的快速回复 - 我基于 codex.wordpress.org/Function_Reference/wp_enqueue_script,所以是的,应该调用 add_action。代码 sn-p 来自我正在构建的插件。
  • 你已经注册了他们,但没有入队..

标签: javascript php wordpress


【解决方案1】:

在评论中点赞 - 您已注册它们,但尚未入队...

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() 的情况下工作 - 但反之则不然。

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

【讨论】:

  • 感谢 Obmerk 并感谢其他所有人的 cmets。第一次使用stackoverflow,不得不说确实印象深刻!!
  • @Ian 你很受欢迎。这是学习和进步的最佳方式 - 通过解决其他人的问题..
猜你喜欢
  • 2013-12-20
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多