If you want to remove categories from the breadcrumb trail on WooCommerce product pages, there’s a simple solution using a custom WordPress hook. Breadcrumbs are great for improving navigation and SEO, but sometimes you might not want to show certain elements, like categories, especially on product pages where they may clutter the trail.
In this post, I’ll show you how to customize the AIOSEO breadcrumbs so that categories are hidden only on WooCommerce product pages, while remaining visible for other content types like blog posts.
Here’s the code you can add to your theme’s functions.php file:
add_filter( 'aioseo_breadcrumbs_trail', 'remove_category_from_aioseo_breadcrumbs_trail' );
function remove_category_from_aioseo_breadcrumbs_trail( $crumbs ) {
if ( is_product() ) {
foreach ( $crumbs as $key => $crumb ) {
if ( is_a( $crumb['reference'], 'WP_Term' ) ) {
unset( $crumbs[ $key ] );
}
}
return array_values( $crumbs );
}
return $crumbs;
}
Leave a Reply