With this new snippet, you will be able to modify the breadcrumb trail on WooCommerce product pages and hide certain specific categories in the taxonomy lists on the public site (but not in the administration). This allows you to clean up or filter the navigation visible to users without affecting the admin side management.
Here's how to hide specific categories using a simple code snippet inserted into your WordPress theme. Once enabled, the specified categories are invisible to the public (navigation, menus, category filters, etc.), but remain accessible in the WooCommerce back office for product administration.
Result: a personalized breadcrumb, clean and without unwanted categories, but still functional for SEO and UX navigation.
1|Information
Before using this snippet, make sure you have installed the Code Snippets plugin (free) on your WordPress site. If not, you can download it via the link below. If you have never added a specific feature to your WordPress site, we recommend you start by reading our dedicated guide (link below).
2|Prérequis
Here is the list of extensions required for this snippet to work properly. Make sure to install them before activating the snippet.
WooCommerce | Necessary for selling virtual products on your site |
3|Snippet
Here is the snippet to use. The comments inside the code will guide you on how this snippet works.
Assurez-vous de cocher la case « Run snippet everywhere » avant d’activer le snippet.
/** Here's how to hide specific categories using a simple code snippet that you can insert into your WordPress theme. Once activated, the specified categories will be invisible to the public (navigation, menus, category filters, etc.), but will remain accessible in the WooCommerce back office for product administration. */
<?php
add_filter(
"woocommerce_get_breadcrumb",
"disableBreadCrumbCategories",
20,
2
);
function disableBreadCrumbCategories($crumbs, $breadcrumb)
{
if (!is_product()) {
return $crumbs;
}
global $post;
$product = wc_get_product($post->ID);
if (!$product) {
return $crumbs;
}
// Categories to Exclude from Breadcrumn
$excl_slugs = ["cat-1", "cat-2", "cat-3"];
// Product Categories
$terms = get_the_terms($post->ID, "product_cat");
if (!$terms || is_wp_error($terms)) {
return $crumbs;
}
$selected_term = null;
foreach ($terms as $term) {
if (!in_array($term->slug, $excl_slugs)) {
$selected_term = $term;
break;
}
}
// If all categories are excluded, do nothing
if (!$selected_term) {
return $crumbs;
}
// Rebuild Terms Hierarchy
$hierarchy = [];
$term_id = $selected_term->term_id;
while ($term_id != 0) {
$term = get_term($term_id, "product_cat");
if (!$term || is_wp_error($term)) {
break;
}
if (!in_array($term->slug, $excl_slugs)) {
$hierarchy[] = [$term->name, get_term_link($term)];
}
$term_id = $term->parent;
}
// Reverse to get the parent category first
$hierarchy = array_reverse($hierarchy);
// Rebuild the Breadcrumb
$new_crumbs = [];
// Keep Home and Shop
foreach ($crumbs as $crumb) {
if (in_array($crumb[0], ["Home", "Shop"])) {
$new_crumbs[] = $crumb;
}
}
// Add Categories
$new_crumbs = array_merge($new_crumbs, $hierarchy);
// Add Product Name
$last = end($crumbs);
$new_crumbs[] = $last;
return $new_crumbs;
}
function hideWooCommerceCategories($terms, $taxonomies, $args)
{
if (is_admin()) {
return $terms;
}
$excl_slugs = ["cat-1", "cat-2", "cat-3"];
if (in_array("product_cat", (array) $taxonomies)) {
foreach ($terms as $key => $term) {
if ( !is_int($term) ) {
if (in_array($term->slug, $excl_slugs)) {
unset($terms[$key]);
}
}
}
}
return $terms;
}
add_filter("get_terms", "hideWooCommerceCategories", 10, 3);
Why is this code useful?
- UX/SEO control: do not display technical, internal or redundant categories.
- Cleaned up the breadcrumbs for better readability.
- Noise reduction in user-side filters, menus or archive pages.
Take it a step further
This type of customization demonstrates how WooCommerce can be extended through WordPress filters and actions. If you'd like to further customize your loyalty program, consider consulting the WooRewards documentation or hiring an experienced WordPress developer (see our Custom Development page).
Our Plugins
We have created powerful and widely acclaimed plugins for WooCommerce. Boost your sales with our solutions
WooRewards
Discover the most powerful loyalty plugin for WooCommerce. Simple or tiered systems, referrals, social networks, badges and achievements, you will find all the tools to build YOUR loyalty program
Learn MoreVIP Membership
VIP Memberships is a complete membership management tool for your WooCommerce site. Sell subscriptions to your customers and offer them benefits such as preferential prices or exclusive products.
Learn MoreVirtual Wallet
Offer your customers a virtual wallet on your website. Let them save money by purchasing your products and use this credit on future purchases. This extension also offers a complete gift card tool
Learn MoreReferral Codes
Win new customers with this complete SEO tool. Whether through influencers or simple referrers, reward them and the new customers they bring
Learn More