简体   繁体   English

如何将JS添加到Wordpress中并引用CSS或其他脚本?

[英]How to add JS to Wordpress and reference CSS or other scripts?

so I am working on a custom theme in Wordpress. 因此我正在使用Wordpress中的自定义主题。 I used to have a whole lot of JS in the head.php , like this: 我过去在head.php中有很多JS,像这样:

jQuery(document).ready(function($) {

    $('.widgetblock').accordion({
        //args
    });


    if ( have_posts() ) {
        //stuff
    }
});

However I've since read that the proper way to add JS in Wordpress is to use wp_enqueue_script() in functions.php , so I'm trying to do that. 但是,我从那以后已经读到,在Wordpress中添加JS的正确方法是在functions.php中使用wp_enqueue_script() ,所以我正在尝试这样做。 What I've done is just put that same code into a .js file that I've save in the corresponding folder, and I'm then loading it like this: 我要做的只是将相同的代码放入保存在相应文件夹中的.js文件中,然后像这样加载它:

function script_assets() {
    wp_enqueue_script( 'js-code', get_template_directory_uri() . '/js/js-code.js', array( 'jquery', 'jquery-ui-widget' ) );
}

add_action( 'wp_enqueue_scripts', 'script_assets' );

I've added JQuery and JQuery UI as dependencies. 我已将JQuery和JQuery UI添加为依赖项。

However, that doesn't work. 但是,这不起作用。 Chrome says that accordion() and have_posts() aren't defined. Chrome表示未定义手风琴()和have_posts()。 I also encounter issues if I try to reference specific elements from my CSS. 如果我尝试从CSS中引用特定元素,也会遇到问题。

So, what am I to do? 那么,我该怎么办? This is probably a very basic problem, but I can't find how to solve it. 这可能是一个非常基本的问题,但是我找不到解决方法。

Another problem is, how do I make 100% sure that some JS scripts are loaded after the CSS? 另一个问题是,如何100%确保CSS之后加载了某些JS脚本? I'm using -prefix-free which should be loaded right after it, but I'm not sure that's possible with wp_enqueue... EDIT: I can't really load that script in the footer, it needs to be loaded immediately after the CSS. 我正在使用-prefix-free ,应在其后立即加载,但我不确定wp_enqueue是否有可能...编辑:我无法真正在页脚中加载该脚本,需要在加载后立即加载CSS。 :/ :/

Thanks. 谢谢。

Every time I have had to add jQuery to a WordPress site I use this code that I found on CSS-tricks 每次不得不将jQuery添加到WordPress网站时,我都会使用在CSS技巧中找到的这段代码

if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
 function my_jquery_enqueue() {
 wp_deregister_script('jquery');
 wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false,    null);
 wp_enqueue_script('jquery');
}

For what I understand, once you load jQuery this way you prevent conflicts and it gets loaded only once. 据我了解,一旦以这种方式加载jQuery,就可以防止冲突,并且仅加载一次。

Well someone else posted an answer but then deleted it so... 好吧,其他人发布了答案,但随后将其删除,因此...

The dependency I was using here wp_enqueue_script( 'js-code', get_template_directory_uri() . '/js/js-code.js', array( 'jquery', 'jquery-ui-widget' ) ); 我在这里使用的依赖wp_enqueue_script( 'js-code', get_template_directory_uri() . '/js/js-code.js', array( 'jquery', 'jquery-ui-widget' ) ); was wrong; 错了; I should have used jquery-ui-accordion, which jquery-ui-widget doesn't include. 我应该使用jquery-ui-accordion,而jquery-ui-widget不包含。 I elected to include the whole of jquery-ui (so I could use other features without having to add specific dependencies for each of them) but to do that I had to link to Google's, since there is no full package in WP. 我选择包括整个jquery-ui(这样我就可以使用其他功能,而不必为每个功能添加特定的依赖项),但是要做到这一点,我必须链接到Google的功能,因为WP中没有完整的软件包。

That fixed some of my issues, not everything (the -prefix-free script still isn't loaded right after the stylesheet, and I have to find a way to replace the PHP functions that I used before) but the website's working, so there's that :) 这解决了我的一些问题,而不是所有问题(-prefix-free脚本仍未在样式表之后立即加载,并且我必须找到一种方法来替换之前使用的PHP函数),但是网站可以正常工作,因此那:)

Thanks to whomever actually answered, I don't remember your username, sorry. 感谢实际回答的任何人,对不起,我不记得您的用户名了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM