Z
Z
zeaovede2022-02-14 05:24:10
PHP
zeaovede, 2022-02-14 05:24:10

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 ) )) {
...
}

Ready.
Another 9 pages have other scripts and styles, connected according to the same scheme. Ready.
And then I needed to connect styles and scripts for the page that is responsible for displaying all the posts (the blog page that is displayed through the settings - reading). I have a standard wordpress template and index.php is responsible for the output.

The first thing I encountered was the lack of an id for this template.
Again I do everything according to the old scheme, only without specifying the page id. Ready.
I look into the page code from 1 to 19 and now I see duplicate scripts and styles. In general, a complete hat. Scripts and styles are connected for all pages and for the given id. I chose this tactic so as not to load individual pages with unnecessary scripts and styles, but then everything went wrong

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
Zhainar, 2016-02-22
@zhainar

imageSaveAlpha($image, true);

A
Artem Zolin, 2022-02-14
@zeaovede

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
IvanMogilev, 2022-02-14
@IvanMogilev

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 question

Ask a Question

731 491 924 answers to any question