PlainH2O
PlainH2O

Reputation: 167

conditional tag (is_shop(), is_product_category() and is_product())

I want to enqueue js and css for product category, shop and single product pages only. The below works but I wonder if there is a better way to do it?

function my_enqueue_stuff() {
        if(is_shop() or is_product() or is_product_category())
        {
            wp_enqueue_script( 'style-css', get_stylesheet_directory_uri() . '/assets/css/style.css');
        }
        }
        add_action( 'wp_enqueue_scripts', 'my_enqueue_stuff' );

On the same token, what is the correct way to use !? Below doesn't work.

function not_needed(){
  if(!is_shop() or !is_product() or !is_product_category()){
     return;
  }

Upvotes: 1

Views: 1652

Answers (1)

Bhautik
Bhautik

Reputation: 11282

Try the below code.

function my_enqueue_stuff() {
    if( is_shop() || is_product() || is_product_category() ) {
        wp_enqueue_script( 'style-css', get_stylesheet_directory_uri() . '/assets/css/style.css');
    }
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_stuff' );

UPDATE as per OP Request

function my_enqueue_stuff() {
    if( !is_shop() && !is_product() && !is_product_category() ) {
        wp_enqueue_script( 'style-css', get_stylesheet_directory_uri() . '/assets/css/style.css');
    }
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_stuff' );

Upvotes: 1

Related Questions