Skip to content

Instantly share code, notes, and snippets.

@jorpdesigns
Created July 6, 2021 14:32
Show Gist options
  • Save jorpdesigns/771f87497e041e659f5befe1b567338f to your computer and use it in GitHub Desktop.
Save jorpdesigns/771f87497e041e659f5befe1b567338f to your computer and use it in GitHub Desktop.
Snippet to modify breadcrumbs on WooCommerce product page
<?php
add_filter( 'woocommerce_get_breadcrumb', 'custom_breadcrumb_urls', 20, 2 );
function custom_breadcrumb_urls( $crumbs, $breadcrumb ){
$customCrumbs = array();
foreach ($crumbs as $key => $crumb) {
$crumbLabel = $crumb[$key][0];
$crumbURL = $crumb[$key][1];
// SET CONDITIONS FOR NEW BREADCRUMB e.g. remove second level items
if ( ($crumbLabel !== __('Shop', 'Woocommerce')) && ($crumbLabel !== __('Primary Category 1', 'Woocommerce')) && ($crumbLabel !== __('Primary Category 2', 'Woocommerce')) ) {
$customCrumbs[] = $crumb;
}
}
foreach($customCrumbs as $key => $crumb){
switch( $crumb[0] ) { // Modify second level crumb items
case "Crumb Label" : // If second level crumb item label is Crumb Label
$customCrumbs[$key][0] = 'Crumb new name'; // Set new label
$customCrumbs[$key][1] = '/crumb-new-url/'; // Set new URL
break;
}
}
return $customCrumbs;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment