To show only the categories that have thumbnails on the WooCommerce shop page, you can use the following code in your theme’s functions.php file:
add_filter( 'woocommerce_product_subcategories_args', 'show_categories_with_thumbnails' ); function show_categories_with_thumbnails( $args ) { $thumbnail_categories = array(); // Initialize an empty array to store the categories with thumbnails $all_categories = get_terms( 'product_cat', array( 'hide_empty' => false ) ); // Get all product categories foreach ( $all_categories as $category ) { $thumbnail_id = get_term_meta( $category->term_id, 'thumbnail_id', true ); if ( $thumbnail_id ) { $thumbnail_categories[] = $category->term_id; // Add category to array if it has a thumbnail } } $args['include'] = $thumbnail_categories; // Only include categories with thumbnails return $args; }
This code uses the get_terms
function to get all the product categories and then checks if each category has a thumbnail using the get_term_meta
function. If a category has a thumbnail, it is added to an array of categories with thumbnails.
The include
argument in the $args
array is set to the array of category IDs with thumbnails, which will result in only those categories being displayed on the shop page.
This code will show only the categories that have thumbnails on the shoppage. Categories without thumbnails will be hidden.
0 Lời bình