Skip to content

Instantly share code, notes, and snippets.

@mehrshaddarzi
Created May 14, 2025 05:56
Show Gist options
  • Save mehrshaddarzi/620fe4e0f36125c29de0d88f6f53d5d9 to your computer and use it in GitHub Desktop.
Save mehrshaddarzi/620fe4e0f36125c29de0d88f6f53d5d9 to your computer and use it in GitHub Desktop.
Woodmart Css Post class
<?php
// Remove Image Classes
// using function to add class to `the_post_thumbnail()`
// https://wordpress.stackexchange.com/questions/102158/add-class-name-to-post-thumbnail
add_filter('wp_get_attachment_image_attributes','filter_the_post_thumbnail_remove_class');
function filter_the_post_thumbnail_remove_class($attr) {
if(is_admin() || wp_doing_ajax()) {
return $attr;
}
//if(is_front_page()) {
if(isset($attr['class']) and !empty($attr['class'])) {
$attr['class'] = str_replace(["size-woocommerce_thumbnail", "attachment-woocommerce_thumbnail", "attachment-full", "attachment-large", "attachment-medium"], "", $attr['class']);
}
if(empty(trim($attr['class']))) {
unset($attr['class']);
}
//}
return $attr;
}
// Remove Post Class For Posts
add_filter( 'post_class', 'filter_hook_post_class', 60, 3 );
function filter_hook_post_class($array, $css_class, $post_id) {
if(is_admin() || wp_doing_ajax()){
return $array;
}
//if(is_front_page()) {
$list = ['post-'.$post_id, 'has-post-thumbnail', 'type-post', 'status-publish', 'format-standard'];
$exist = false;
foreach($array as $index => $class) {
if(in_array($class, $list)) {
unset($array[$index]);
$exist = true;
}
if(substr($class, 0 , 9) == "category-") {
unset($array[$index]);
$exist = true;
}
}
if($exist) {
$array = array_values($array);
}
//}
return $array;
}
// Remove Post Class for Product
add_filter( 'woocommerce_post_class', 'filter_hook_wc_product_post_class', 50, 2 );
function filter_hook_wc_product_post_class($array, $product) {
if(is_admin() || wp_doing_ajax()){
return $array;
}
//if(is_front_page()) {
$list = ['post-'.$product->get_id(), 'has-post-thumbnail', 'featured', 'shipping-taxable', 'purchasable', 'product-type-simple', 'status-publish', 'instock', 'product-type-variable', 'type-product', 'sale'];
$exist = false;
foreach($array as $index => $class) {
if(in_array($class, $list)) {
unset($array[$index]);
$exist = true;
}
if(substr($class, 0 , 12) == "product_cat-") {
unset($array[$index]);
$exist = true;
}
}
if($exist) {
$array = array_values($array);
}
//}
return $array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment