Skip to content

Instantly share code, notes, and snippets.

@WhiteHatJoker
Created October 18, 2021 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WhiteHatJoker/29f0b6fbba3dd4f6397908aca1658a38 to your computer and use it in GitHub Desktop.
Save WhiteHatJoker/29f0b6fbba3dd4f6397908aca1658a38 to your computer and use it in GitHub Desktop.
If you have set the WooCommerce Settings -> Shop Page Display to show categories & subcategories, you may notice that your base shop page automatically shows all parent categories. This PHP snippet maybe useful if you would like to remove certain categories from the view on shop page.

Excluding certain categories from the base shop page

If you have set the WooCommerce Settings -> Shop Page Display to show categories & subcategories, you may notice that your base shop page automatically shows all parent categories. This PHP snippet maybe useful if you would like to remove certain categories from the view on shop page.

Installation

  1. Copy over the code in the current repository to your WordPress functions.php or woo-config.php files.
  2. Go to your Wordpress Admin Panel -> Products -> Categories and note down the slugs of categories that you would like to hide on your shop page.
  3. Place the noted slugs into the array of slugs on line 14 of functions.php
/* Exclude Certain Categories from Shop Base*/
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
function get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
// if 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( 'slugs', 'of', 'categories', 'you', 'would', 'like', 'be-hidden', 'on-shop' ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment