问题
Woocommerce adds the assigned product categories and attributes as custom classes to li.product
<li class="post-120 product type-product status-publish has-post-thumbnail
category1 category2 category3 category4 pa_one pa_two...">
We have assigned quite a lot of categories to each product and it slows down the site. Is there a way to remove those additional classes?
回答1:
If you know php, You can edit the woocoommerce php template file to remove those additional classes. If you opened the file, there is a php variable to be added additional classes and it is a array.
回答2:
In the woocommerce plugin '3.6.2' the file 'wc-template-functions.php' it says:
// Note, to change classes you will need to use the newer woocommerce_post_class filter.
So to remove 'first' & 'last' classes I used:
add_filter( 'woocommerce_post_class', 'remove_post_class', 21, 3 ); //woocommerce use priority 20, so if you want to do something after they finish be more lazy
function remove_post_class( $classes ) {
if ( 'product' == get_post_type() ) {
$classes = array_diff( $classes, array( 'last','first' ) );
}
return $classes;
}
For product categories I used:
add_filter( 'product_cat_class', 'remove_category_class', 21, 3 ); //woocommerce use priority 20, so if you want to do something after they finish be more lazy
function remove_category_class( $classes ) {
if ( 'product' == get_post_type() ) {
$classes = array_diff( $classes, array( 'last','first' ) );
}
return $classes;
}
回答3:
I would suggest applying a filter to the post_class function instead of editing the template as woocommerce updates these regularly and can be a pain to keep on top of them. To do this you could check to see if you in the product loop or if a product exists to also remove from the product page like this :
add_filter( 'post_class', function($classes){
$product = wc_get_product();
return ($product) ? array('product' 'or-any-class-you-want') : $classes; } , 9999 );
来源:https://stackoverflow.com/questions/48546487/how-to-remove-classes-added-to-woocommerce-li-product