How to load jQuery into WordPress properly

前端 未结 4 1865
臣服心动
臣服心动 2020-12-16 06:59

I have a WordPress site that I would like to use a jQuery plugin called DataTables. I\'ve been having troubling figuring out the right way to load the jQuery script and the

4条回答
  •  臣服心动
    2020-12-16 07:33

    Best way to do this is through the functions.php file,

    URL: http://codex.wordpress.org/Function_Reference/wp_enqueue_script

    Sample:

    /**
     * Proper way to enqueue scripts and styles
     */
    function theme_name_scripts() {
        wp_enqueue_style( 'style-name', get_stylesheet_uri() );
        wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
    }
    
    add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
    

    What this code reads is that you are creating a function called "theme_name_scripts", now you are calling wp_enqueue_script(), this takes 5 parameters "Script Name", "URL","ARRAY","Version","True/False" (True loads in the Header, and False in the Footer)...

    Add_action part is a hook to attach this to the WordPress. It is self explanatory, you need it in order to make the function work. It says wp_enqueue_scripts from function 'theme_name_scripts' or the function name..

    Best of luck, if you need more help try Googleing "WordPress, enqueue Scripts tutorial"

提交回复
热议问题