This topic has 4 replies, 2 voices, and was last updated 10 months ago ago by Luca Rossi
I made a snippet for dynamic breadcrumbs, maybe you can make an option or elementor widget or something out of it. It uses the referer to build the breadcrumbs instead of the primary category. If someone comes from google or somehow directly on the product page, it uses the primary category. This is very handy when you use products in multiple categories.
I am doing this here because in the feature request I cannot post code somehow.
For safety reasons I will first place it in the private content area.
The only thing I ask is if you can check if my code is correct (so far it works great, but you guys hold the knowledge). It’s online on my staging area, but not on my production area yet.
Dear Nancy,
We hope this message finds you well.
We are pleased to inform you that we have made some updates to your custom code. Your contribution is greatly appreciated, and we would like to invite you to upload the revised code to Pastebin (https://pastebin.com/). Following this, please proceed to create a new feature request on our taskboard (https://www.8theme.com/taskboard/) for further consideration.
Below is the updated code snippet for your reference:
add_filter('woocommerce_get_breadcrumb', 'custom_woocommerce_breadcrumbs', 10, 2);
function custom_woocommerce_breadcrumbs($crumbs, $breadcrumb) {
if (is_singular('product')) {
$referer = wp_get_referer();
if (!empty($referer)) {
// Parse the referer URL to get the path
$parsed_url = parse_url($referer);
$path_segments = explode('/', trim($parsed_url['path'], '/'));
$custom_crumbs = [$crumbs[0]]; // Home link
// Build custom breadcrumb from referer
if (!empty($path_segments)) {
foreach ($path_segments as $segment) {
$term = get_term_by('slug', $segment, 'product_cat');
if ($term) {
$custom_crumbs[] = [$term->name, get_term_link($term)];
}
}
}
// Add the current product to the custom breadcrumb
$custom_crumbs[] = end($crumbs);
return $custom_crumbs;
}
}
// Return default breadcrumbs for other cases
return $crumbs;
}
We thank you once again for your valuable input and look forward to your continued collaboration.
Warm regards,
The 8Theme Team
Thank you for your adjustments!
I placed the code on pastebin and made a topic on the feature request.
I also updated the code, excluding the default category from the breadcrumbs (often called uncategorized).
Thanks @Nancy.
Let’s see how the vote goes!
You must be logged in to reply to this topic.Log in/Sign up