John Davidson

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

0 comments
Message:


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;
}


My Answer:
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' );

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

Rate this post

5 of 5 based on 1493 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us