Solution for dynamic breadcrumbs (with code)

This topic has 4 replies, 2 voices, and was last updated 2 months, 2 weeks ago ago by Luca Rossi

  • Avatar: Nancy
    Nancy
    Participant
    February 21, 2024 at 10:10

    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.

    Please, contact administrator
    for this information.
    3 Answers
    Avatar: Justin
    Luca Rossi
    Support staff
    February 21, 2024 at 11:59

    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

    Avatar: Nancy
    Nancy
    Participant
    February 21, 2024 at 12:31

    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).

    Avatar: Justin
    Luca Rossi
    Support staff
    February 21, 2024 at 13:08

    Thanks @Nancy.

    Let’s see how the vote goes!

  • Viewing 4 results - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.Log in/Sign up

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.