php - Remove generated category and tag class names from woocommerce product & blog listings markup

admin2025-01-08  3

I noticed in my wordpress/woocommerce setup that every time i add a tag to a product or a blog post. It adds that tag as a class into the listed item (Product/blog)

I also noticed it adds a class in the same place for every category i put these post items into.

How can i prevent wordpress and woocommerce from addings these tag and category names into my html markup as classes? As i do not need them and its creating a mess.

Thanks for any help! I could not locate a similar function anywhere.

I noticed in my wordpress/woocommerce setup that every time i add a tag to a product or a blog post. It adds that tag as a class into the listed item (Product/blog)

I also noticed it adds a class in the same place for every category i put these post items into.

How can i prevent wordpress and woocommerce from addings these tag and category names into my html markup as classes? As i do not need them and its creating a mess.

Thanks for any help! I could not locate a similar function anywhere.

Share Improve this question asked Feb 15, 2019 at 9:47 Ian GarveyIan Garvey 1111 silver badge7 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

You have to leverage the woocommerce wc_product_post_class filter. The following code should do the trick.


function my_strip_tag_class( $classes ) {

    global $post;
    $product_tags = get_the_terms( $post->ID, 'product_tag' );

    if( $product_tags ) foreach ( $product_tags as $tag ) {

        $tag_index = array_search($tag, $classes);

          if($tag_index !== false){
            unset($classes[$tag_index]);
        }

    }
    return $classes;
}
add_filter( 'wc_product_post_class', 'my_strip_tag_class' );

For WooCommerce 3.6.2+ try the woocommerce_post_class filter:

add_filter('woocommerce_post_class', function($classes, $product) {
    // remove whatever classes you want here...
    return $classes;
}, 10, 2);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736267509a1201.html

最新回复(0)