Answer the question
In order to leave comments, you need to log in
Who connects scripts and styles on their wordpress and how?
I have a website with 20 pages. 10 of them have the same styles and connection scripts.
My connection:
function main_style() {
if ( is_page ( array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ) )) {
...
}
Answer the question
In order to leave comments, you need to log in
is_home()
- checks if the page with the latest posts is shown.
I want to note the function get_theme_file_uri()
, which is rarely used due to a bunch of old guides. It searches for the file first in the child theme, if it is not there, then it takes it from the main one
. As the version of the file, it is convenient to pass the time of its last modification with the fourth parameter filemtime()
. This is necessary so that the browser does not cache changed files and always shows only the current version
. I often change something programmatically, then my id may change, but I follow the slugs for SEO, so I check the is_page()
slugs of posts in all type checks. But that's a matter of taste
function custom_scripts_init() {
// общие стили
wp_enqueue_style( 'common-styles', get_theme_file_uri( 'assets/css/common.min.css' ) , array(), filemtime( get_theme_file_path( '/assets/css/common.min.css' ) ) );
// общие скрипты
wp_enqueue_script( 'common-scripts', get_theme_file_uri( 'assets/js/common.min.js' ), array( 'jquery' ), filemtime( get_theme_file_path( '/assets/js/common.min.js' ) ), true );
// скрипты для страницы постов
if ( is_home() ) {
wp_enqueue_script( 'home-scripts', get_theme_file_uri( 'assets/js/home.min.js' ), array( 'jquery' ), filemtime( get_theme_file_path( '/assets/js/home.min.js' ) ), true );
}
// скрипты для массива страниц
if ( is_page( ['sample-page', 'contacts'] ) ) {
wp_enqueue_script( 'page-scripts', get_theme_file_uri( 'assets/js/page.min.js' ), array( 'jquery' ), filemtime( get_theme_file_path( '/assets/js/page.min.js' ) ), true );
}
}
add_action( 'wp_enqueue_scripts', 'custom_scripts_init' );
I do the same, I separate ifs in functions, but I still use is_singular is_front_page , etc.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question