0

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.

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' );

  • Hello and thanks for the answer. When trying to apply your code to my functions.php file i get this error "Your PHP code changes were rolled back due to an error on line 638 of file wp-content/plugins/woocommerce/includes/wc-template-functions.php. Please fix and try saving again. Cannot redeclare wc_get_product_class() (previously declared in wp-content/themes/child-theme/functions.php:839)" – KYSSE Feb 15 at 11:38
  • Sorry, I made a terrible mistake in my answer, I will edit it – Fabrizio Mele Feb 15 at 11:43
  • Please let me know once you are done editing it. – KYSSE Feb 15 at 11:54
  • @KYSSE take a look now – Fabrizio Mele Feb 15 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.