1

I have a simple Craft 3 website, with products that are organised in categories.

Can someone please tell me how to do the following:

On the the individual product pages (_entry.html) display a 'breadcrumb' menu that includes the parent category name/url (I've followed the few examples posted here - they don't work).

And also be able to simply display the name of the parent category of the product in text (e.g. to use as part of the h1 title for the product page).

Many thanks for your help - it is very much appreciated!

2
  • Can you share the code you've got for the breadcrumb so far? Commented Jul 12, 2018 at 11:41
  • {% for category in entry.products.all() %} <p>{{ category.title }}</p> {% endfor %}
    – MGX
    Commented Jul 12, 2018 at 13:07

1 Answer 1

1

Assuming your categories are accessible from a Categories field called 'productCategory', and you only have 1 category assigned to each product, you can display the name of the parent category with:

{{ entry.productCategory.one() }}

Or you can loop through multiple Categories with:

{% for category in entry.productCategory.all() %}
    {{ category.title }}
{% endfor %}

And to get the category link for your breadcrumb, you could use something like:

{{ entry.productCategory.one().slug }}
5
  • Firstly, many thanks for taking the time to replay to my question - it is very much appreciated. Sorry, but I'm confused where you've used 'category' as the field name. And I'm not able to get this to work: This, simply to display the parent category of the entry (_entry.html) on the page, where 'products' is the handle of the category group: {% for category in entry.products.all() %} {{ category.title }} {% endfor %} And I'm altogether lost with code for adding a link to use in a breadcrumb. Many thanks!
    – MGX
    Commented Jul 12, 2018 at 13:03
  • @MGX when you are editing your products in the CP, how are you assigning a category to them? I'm guessing/hoping you have a field that you use to set the category... this is what we need to know the name of Commented Jul 12, 2018 at 13:30
  • Yes - a category is assigned to each product via a category field in the CP, the handle for this is 'productCategory'. The handle of the category group is called 'products'. Many thanks.
    – MGX
    Commented Jul 12, 2018 at 15:03
  • @MGX Just edited my answer to use productCategory instead of category, see if that does the trick for you? If not suggesting seeking help on the Craft Slack channel. Commented Jul 12, 2018 at 19:43
  • Yes - that's very helpful. Many thanks for taking the time to help with this, it's very much appreciated. I have a problem with 'clean' URL's at the moment, so I'm getting a 404 error unless the url is in this format: /products/?category=name-of-category'. If the URL is '/products/name-of-category' it fails (404). Not sure what's causing that...routes isn't helping :(
    – MGX
    Commented Jul 13, 2018 at 8:00

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.