WooCommerce:如何在店铺页面隐藏「shoes」分类产品
Hey there! Let's sort out your WooCommerce request—you want to hide all products in the 'shoes' category from your main shop page, only show non-shoes items, and keep shoes on a dedicated separate page. Here are two reliable solutions for you:
This approach is great if you're comfortable working with a bit of code, and it avoids adding extra plugins to your site.
步骤:
- Add the code to your site: You can either paste this into your theme's
functions.phpfile (note: theme updates might overwrite it) or use a code snippet plugin like Code Snippets (safer for long-term use). - Adjust the code if needed: If your "shoes" category uses a different slug, replace
'shoes'with your actual category slug. If you know the category ID, you can skip theget_term_byline and directly use the ID number.
add_action( 'pre_get_posts', 'exclude_shoes_category_from_shop' ); function exclude_shoes_category_from_shop( $query ) { // Only target the front-end main shop page product query if ( ! is_admin() && $query->is_main_query() && is_shop() ) { // Get the ID of the "shoes" product category $shoes_cat = get_term_by( 'slug', 'shoes', 'product_cat' ); if ( $shoes_cat ) { $shoes_cat_id = $shoes_cat->term_id; // Set up the tax query to exclude this category $tax_query = $query->get( 'tax_query', array() ); $tax_query[] = array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => $shoes_cat_id, 'operator' => 'NOT IN', ); $query->set( 'tax_query', $tax_query ); } } }
额外提示:
To create your dedicated shoes page, make a new page in WordPress and add this shortcode:
[products category="shoes"]
Or use the block editor to add a "Products by Category" block and select the "shoes" category—this will display only your shoes products on that page.
Perfect if you don't want to mess with code at all. There are several free WooCommerce plugins that let you exclude categories from the shop page easily:
推荐插件 & 操作步骤:
WooCommerce Exclude Categories from Shop:
- Install and activate the plugin from the WordPress plugin repository.
- Go to
WooCommerce > Settings > Exclude Categories. - Under "Exclude Categories from Shop", find and check the "shoes" category.
- Save your changes—your shop page will now stop showing any shoes products.
Alternative: WooCommerce Product Filters:
This plugin offers more advanced filtering options, including category exclusion. Once activated, navigate to its settings and set up a rule to exclude the "shoes" category from the main shop page.
鞋子专属页面设置:
Same as the code method—create a new page and use the [products category="shoes"] shortcode or the "Products by Category" block to display only shoes products.
内容的提问来源于stack exchange,提问作者SIlencer Halloul

