E
E
Evgeniy2021-08-23 05:25:05
WordPress
Evgeniy, 2021-08-23 05:25:05

How to connect scripts and styles separately on different WordPress pages?

Good morning everyone (◠‿◠)

In the project:
- 2 pages of categories for entries (News, Projects)
- WooCommerce online store

Separate styles and scripts are connected for each template because it is necessary
But the error is that the scripts and styles applied to the pages of categories of posts and the posts themselves,
are displayed on the pages of the store. (⊙▂⊙)

How can I fix this? ¯\_(o_O)_/¯

// ================================= СТРАНИЦЫ НОВОСТИ И ПРОЕКТЫ
    if ( is_archive() || is_single() ) { 
    //if ( is_archive(array('24', '25')) || is_single() ) { 
    //if ( is_page_template(array('archive.php', 'single-news.php')) ) {   
    //if ( is_archive() || is_single() || ! is_woocommerce() || ! is_cart() || ! is_checkout() || ! is_account_page() ) {  
    
        //подключаем стиль в header
       wp_enqueue_style('bkm-archive-css', get_theme_file_uri('css/news-project.min.css')); 

        //подключаем скрипт в подвале
       wp_enqueue_script('bkm-archive-js', get_theme_file_uri('js/news-project.min.js'), array(), '', true);  
    }

    // ================================= СТРАНИЦЫ WOOCOMMERCE
    if ( is_woocommerce() || is_cart() || is_checkout() || is_account_page() ) { 
    //if ( ! is_single() || ! is_archive() || is_woocommerce() || is_cart() || is_checkout() || is_account_page() ) { 
        //подключаем стиль в header
        wp_enqueue_style('bkm-woocommerce-css', get_theme_file_uri('css/shop.min.css')); 

        //подключаем скрипт в подвале
        wp_enqueue_script('bkm-woocommerce-js', get_theme_file_uri('js/shop.min.js'), array(), '', true); 
        wp_enqueue_script('bkm-woocommerce-js', get_theme_file_uri('js/add-to-cart.min.js'), array(), '', true); 
    }


I tried to specify the is_page_template template for articles, and only is_archive() || is_single() , and specify an ID in is_archive() || is_single() , anyway.... ಠ_ಠ

When through is_page_template , extra styles disappear from woocommerce pages, but everything also flies on article pages.. (╥_╥)
Other options just don't work, styles in posts and categories are only their own , in woocommerce all together...
(︶︹︺)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Zolin, 2021-08-23
@Zheleznov

It's easiest to check post_type to apply styles for a specific post type. This condition will work for both archive and single pages.

if ( get_post_type() === 'post' ) {
  # code...
}

As for your task, you will need a compound condition. You need to make sure that you are in one of two categories or that the single post has one of two categories:
if ( is_category( [ 'news', 'projects' ] ) || ( is_singular( 'post' ) && has_category( [ 'news', 'projects' ] ) ) ) { 
  # code...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question