Dieser kurze Beitrag zeigt, wie man in WooCommerce Kategorien auf Shop-Seiten und in der Produkt-Suche verstecken kann. Wir benutzen dazu das folgende Code Snippet:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
add_filter( 'get_terms', 'ts_get_subcategory_terms', 10, 3 ); add_action( 'pre_get_posts', 'remove_categories_from_search_results',99 ); function ts_get_subcategory_terms( $terms, $taxonomies, $args ) { $new_terms = array(); // if it is a product category and on the shop page if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) { foreach( $terms as $key => $term ) { if ( !in_array( $term->slug, array( 'uncategorised','unkategorisiert' ) ) ) { $new_terms[] = $term; } } $terms = $new_terms; } return $terms; } function remove_categories_from_search_results( $query ) { if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) { $query->set( 'post_type', array( 'product' ) ); $tax_query = array( array( // likely what you are after 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => array('uncategorised', 'unkategorisiert'), 'operator' => 'NOT IN', ), ); $query->set( 'tax_query', $tax_query ); } } |
Die zu versteckenden WooCommerce-Kategorien müssen den Arrays in Zeile 9 und 30 hinzugefügt werden.
