https://developer.wordpress.org/themes/basics/including-css-javascript/
Rather then loading the stylesheet in your header.php file, you should load it in using wp_enqueue_style. In order to load your main stylesheet, you can enqueue it in functions.php
To enqueue style.css
wp_enqueue_style( ‘style’, get_stylesheet_uri() );
This will look for a stylesheet named “style” and load it.
The basic function for enqueuing a style is:
wp_enqueue_style( $handle, $src, $deps, $ver, $media);
You can include these parameters:
- $handle is simply the name of the stylesheet.
- $src is where it is located. The rest of the parameters are optional.
- $deps refers to whether or not this stylesheet is dependent on another stylesheet. If this is set, this stylesheet will not be loaded unless its dependent stylesheet is loaded first.
- $ver sets the version number.
- $media can specify which type of media to load this stylesheet in, such as ‘all’, ‘screen’, ‘print’ or ‘handheld.’
wp_enqueue_style( ‘slider’, get_template_directory_uri() . ‘/css/slider.css’,false,’1.1’,’all’);
——————-
Enqueue your script:
wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer);
- $handle is the name for the script.
- $src defines where the script is located.
- $deps is an array that can handle any script that your new script depends on, such as jQuery.
- $ver lets you list a version number.
- $in_footer is a boolean parameter (true/false) that allows you to place your scripts in the footer of your HTML document rather then in the header, so that it does not delay the loading of the DOM tree.
Your enqueue function may look like this:
wp_enqueue_script( ‘script’, get_template_directory_uri() . ‘/js/script.js’, array( ‘jquery’), 1.1, true);
=====================
混合使用:
functionadd_theme_scripts() {
wp_enqueue_style( ‘style’, get_stylesheet_uri() );
wp_enqueue_style( ‘slider’, get_template_directory_uri() . ‘/css/slider.css’, array(), ‘1.1’, ‘all’);
wp_enqueue_script( ‘script’, get_template_directory_uri() . ‘/js/script.js’, array( ‘jquery’), 1.1, true);
if( is_singular() && comments_open() && get_option( ‘thread_comments’) ) {
wp_enqueue_script( ‘comment-reply’);
}
}
add_action( ‘wp_enqueue_scripts’, ‘add_theme_scripts’);
=====================================
