categories - Remove product category placeholder image (Woocomerce)

admin2025-06-05  3

I'm trying to get rid of placeholder product category images on the shop page.

Right now I'm using this code to add a .no-image class to products that don't have an image so I can style them diferently. This works great, I want to do the same thing for categories.

function before_imageless_product() {
if( !has_post_thumbnail( get_the_id() ) ){
    remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_category_thumbnail', 10 );
    echo '<div class="no-product-image">';
}
}
add_action( 'woocommerce_before_shop_loop_item', 'before_imageless_product', 9 );

function after_imageless_product() {
if( !has_post_thumbnail( get_the_id() ) ){
    add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_category_thumbnail', 10 );
    echo '</div>';
}
}
add_action( 'woocommerce_after_shop_loop_item', 'after_imageless_product', 9 );

I've tried to edit the code to detect categories but I can't make it work. What am I doing wrong?

function before_imageless_category() {
    global $wp_query;
    $cat = $wp_query->get_queried_object();
    $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
    $image = wp_get_attachment_url( $thumbnail_id );

    if( !$thumbnail_id ){
        remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_category_thumbnail', 10 );
        echo '<div class="no-category-image">';
    }
}
add_action( 'woocommerce_before_shop_loop_item', 'before_imageless_category', 9 );

function after_imageless_category() {

    global $wp_query;
    $cat = $wp_query->get_queried_object();
    $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
    $image = wp_get_attachment_url( $thumbnail_id );

    if( !$thumbnail_id ){
        add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_category_thumbnail', 10 );
        echo '</div>';
    }

}
add_action( 'woocommerce_after_shop_loop_item', 'after_imageless_category', 9 );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749102215a316378.html

最新回复(0)