1

I'm struggling to get a CSS file to load just on WooCommerce shop archive pages only.

Looking at the documentation I know there is a is_shop() tag.

So I would have thought

function kodr_scripts_styles() {
    if( is_shop() ) {
        wp_enqueue_style( 'wc-shop', get_template_directory_uri() . '/css/shop-only.css', 'all' );
    }
}
add_action( 'wp_enqueue_scripts', 'kodr_scripts_styles' );

Would work fine. But the file doesn't load. I know I can use is_woocommerce() but I don't want this file loading on all WC pages. Any ideas?

alexkodr
  • 523
  • 3
  • 7
  • 30

1 Answers1

0

Try out this code in your theme function.php:-

EDITED:-

function zillion_enqueue_style() {
     if( is_shop() || (is_archive() && is_woocommerce()) ) {
        wp_enqueue_style( 'wc-shop-only', get_template_directory_uri() . '/css/shop-only.css',array(), 'all' );
    }
}
add_action( 'wp_enqueue_scripts', 'zillion_enqueue_style' );
theboss_dev
  • 1,152
  • 1
  • 8
  • 21
  • Thanks for taking time to reply. Can you explain how your code is any different from the function I've already tried with? – alexkodr Nov 07 '22 at 11:25
  • you are calling `wp_enqueue_style()` function but we have to hook it into specific place. `wp_enqueue_scripts()` is the proper hook to use when enqueuing scripts and styles. https://developer.wordpress.org/plugins/hooks/ https://developer.wordpress.org/reference/hooks/wp_enqueue_scripts/ – theboss_dev Nov 07 '22 at 11:53
  • Ok, I already have a hook in place. I just didn't feel the need to add all the code here. Still doesn't work – alexkodr Nov 07 '22 at 13:46
  • can you let me know where you are adding that code? Try adding in function.php – theboss_dev Nov 07 '22 at 13:53
  • functions.php. I've edited my original post above to show exactly how I'm calling that function. So it's clearer. I also have many other css and js files enqueued with no issues. just the if( is_shop() does not want to work. – alexkodr Nov 07 '22 at 16:02
  • you can use other conditional tag for check whether it's archive page and woocommmerce also. try out the edited code i've posted above – theboss_dev Nov 08 '22 at 03:45