Wordpress 向主题添加javascript

Wordpress 向主题添加javascript,wordpress,wordpress-theming,Wordpress,Wordpress Theming,我正在尝试将Nivo滑块代码(而不是WP插件)添加到主题中。我通过将以下内容添加到functions.php中完成了此操作: /* Add scripts for theme to work*/ function theme_add_scripts() { wp_enqueue_script('jquery'); //omit if jQuery already included wp_enqueue_script('nivoslider', bloginfo('stylesheet_

我正在尝试将Nivo滑块代码(而不是WP插件)添加到主题中。我通过将以下内容添加到functions.php中完成了此操作:

/* Add scripts for theme to work*/
function theme_add_scripts() {
  wp_enqueue_script('jquery'); //omit if jQuery already included
  wp_enqueue_script('nivoslider',  bloginfo('stylesheet_url') . '/wp-content/themes/pingst/nivo-slider/jquery.nivo.slider.pack.js', 'jquery', '2.7.1');
  }
add_action('init', 'theme_add_scripts');
看起来应该行得通,但有两个问题

  • 当我手动添加像这样的脚本时,问题是您使用的“”将回显它得到的内容。 使用“”使wp返回而不是回显

    所以在functions.php中更改它

    wp_enqueue_script('nivoslider',  bloginfo('stylesheet_url') . '/wp-content/themes/pingst/nivo-slider/jquery.nivo.slider.pack.js', 'jquery', '2.7.1');
    
    应该是

    wp_enqueue_script('nivoslider',  get_bloginfo('stylesheet_directory') . '/nivo-slider/jquery.nivo.slider.pack.js', 'jquery', '2.7.1');
    

    这应该可以解决问题nr2,也可以解决问题nr1

    主题中header.php的前2-3行是什么?当前,您的站点在doctype之前输出style.css文件。解决这个问题可能是第一个问题。
    wp_enqueue_script('nivoslider',  bloginfo('stylesheet_url') . '/wp-content/themes/pingst/nivo-slider/jquery.nivo.slider.pack.js', 'jquery', '2.7.1');
    
    wp_enqueue_script('nivoslider',  get_bloginfo('stylesheet_directory') . '/nivo-slider/jquery.nivo.slider.pack.js', 'jquery', '2.7.1');