在WordPress中包含并注册Javascript文件函数.php

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在WordPress中包含并注册Javascript文件函数.php相关的知识,希望对你有一定的参考价值。

This code should be added to the functions.php file in the theme's folder. The code will deregister the local copy of jQuery and instead include the google hosted copy. It will also register other scripts.
  1. <?php
  2.  
  3. function my_script_init() {
  4. if (!is_admin()) {
  5. // deregister local copy of jQuery
  6. wp_deregister_script('jquery');
  7. // register google hosted jquery
  8. wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js', false, '1.6.1');
  9. wp_enqueue_script('jquery');
  10.  
  11. // register some other javascript file (just for the sake example)
  12. wp_register_script('otherfile', get_bloginfo('stylesheet_directory').'/scripts/jquery.otherFile.js', array('jquery'));
  13. wp_enqueue_script('otherfile');
  14.  
  15. // register another file that requires that the other two files are loaded first
  16. wp_register_script('mainjs', get_bloginfo('stylesheet_directory').'/scripts/main.js', array('jquery','otherfile'));
  17. wp_enqueue_script('mainjs');
  18.  
  19. wp_deregister_script( 'l10n' );
  20. }
  21. }
  22. add_action('init', 'my_script_init');

以上是关于在WordPress中包含并注册Javascript文件函数.php的主要内容,如果未能解决你的问题,请参考以下文章

PHP 在functions.php中的WordPress中包含和注册Javascript文件

WordPress 是不是在管理页面中包含 jQuery?

在 WordPress 中包含一个 jQuery 插件

Wordpress:在 url 中包含语言变量

如何在 WordPress 主题中包含 jQuery?

在 WordPress 网站中包含汉堡菜单