WP提供了许多条件。因此,与此相反,它将JS和CSS添加到站点上的每个URL
add_action( \'wp_enqueue_scripts\', \'my_scripts_method\' );
function my_scripts_method(){
wp_enqueue_script( \'calc_fun\', \'/wp-content/themes/flatsome-child/assets/js/calc.js\', array(), \'1.6\');
wp_enqueue_style( \'calc_css\', \'/wp-content/themes/flatsome-child/assets/css/calc.css\', true , 9.1 );
}
您将需要这样的东西,它只会将JS和CSS添加到某些URL
add_action( \'wp_enqueue_scripts\', \'my_scripts_method\' );
function my_scripts_method(){
// Only enqueue if this is a single product page that\'s not in product category "example"
// has_term will accept either the name, term ID, or slug of the product category
if(is_singular(\'product\') && !has_term(\'example\', \'product_cat\')) {
wp_enqueue_script( \'calc_fun\', \'/wp-content/themes/flatsome-child/assets/js/calc.js\', array(), \'1.6\');
wp_enqueue_style( \'calc_css\', \'/wp-content/themes/flatsome-child/assets/css/calc.css\', true , 9.1 );
}
}
您可以在其他函数中执行相同的操作,而不是总是执行
get_template_part()
, 将其包装在一个条件中,以检查这是否是要向其中添加代码的特定产品。