1

I have a question related to breadcrumbs. I have a multiple categories with level 3 hierarchy, now if I assign only one category to any product it works for me to create the breadcrumbs based on the category from where user reach to the product page. But when I select multiple categories then its not working for me as it shows the same breadcrumb no matter from which category user reach to the Product page. Is there a way to change the breadcrumbs based on the category path from where a user reach to the product page?

for example:

Shop
    Clothes
        ActiveWear
           Jacket
    Brands
        Adidas
           Jacket

So if user go from the Clothes category the breadcrumb must be Clothes->ActiveWear->Jacket and if user go from the Brands category the breadcrumb must be Brands->Adidas->Jacket

Please let me know if there is a way. Thanks in advance.

3
  • 2
    Please consider rethinking your content model here. Smashing lots of different content types into a single hierarchical category structure severely reduces the flexibility and robustness of your site. Rather, aim to flatten the hierarchy into unique content types and create relationships between those types. In your example, you could have dedicated sections (or category groups) for "Product Types" and "Brands". Your Product Types category group would be fine to have a parent-child relationship from "Clothes > Jackets", as that makes sense ("Jackets" are a more specific type of "Clothes"). Jan 7, 2022 at 14:09
  • 2
    Don't allow the front-end display requirements to dictate the content modelling choices, as this harms the future-proofing of the site. For a more complete discussion of why this matters see here: cld.agency/blog/content-utopia Jan 7, 2022 at 14:11
  • 2
    FYI breadcrumbs are intended to show a relationship between the current page and how the rest of the site is organized, usually it matches the global navigation. I would consider a brand to be more of a “filter” than “category” a product lives in. Typically you link to a manufacturer/brand from a product page anyway so including that path isn’t really useful from a buyer perspective. Ultimately breadcrumbs are not intended to show the trail a user took, like Hansel and Gretel (see point #2). Jan 9, 2022 at 20:15

2 Answers 2

1

You can add Breadcrumbs with the help of craft cms plugins like breadcrumbs and navigations as well they also work well for adding breadcrumbs

0

You will need some way to identify which category the user came from. You could use the referrer header or track the last category the user visited in a session variable, but both of those methods are error-prone and won't play nicely with caching. Instead, I'd include the category as a URL parameter and generate the breadcrumbs based on that. So on your category pages, add the current category as a parameter to the product links.

{% set productUrl = siteUrl(product.getUrl(), params = { from: category.id }) %}

So if category Brands -> Adidas -> Jacket has ID 123, this will result in a link like this: /products/some-jacket?from=123

Then you can use this parameter to diplay the breadcrumb trail for that category.


A broader view: A breadcrumb navigation isn't a good fit for products with multiple categories. While the approach above will work, it might be confusing to the user. If I visit the same product twice, I might end up with two identical product pages with different breadcrumbs. Instead, I would display a list of categories the product belongs to, allowing the user to go back to the overview in any direction they want to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.