1

I have a setup in Craft 2 like below:

Section: products
Url format: flowers/{productCategory.last.uri}/{slug}

Categories: 
cat1 = Bouquets
cat2 = Valentines days
Url format: flowers/{slug}

The problem headache arises when I want to get the entry URL within a category template. It always gets the last category of the entry due to the URL format. So once valentines day is added as a second category then a normal bouquet entry URL goes from this:

/flowers/bouquets/entry-slug

to

/flowers/valentines-day/entry-slug

I solved this partly by making my bouquet URL out of the URL path:

            {% for entry in entries %}
                {% set prodUrl = 'flowers/'~ seg2~ '/'~entry.slug %}
                 <a href="{{ baseUrl }}{{ prodUrl }}" >

However, the entry template now doesn't have the entry model.

How do other people solve this multiple categories for the same content type issue?

1

I can think of a couple possible solutions here:

1. Rethink your url structure
You could always remove the category segment from the url. Maybe create a field for the main category and then another field for all the categories, and use the main category field for your URL. Or you could fetch the first productCategory instead of the last one, so there's less chance of it changing. In my opinion you're going to have a lot of SEO issues if your url changes whenever you add a new productCategory, not to mention broken links and all sorts of other headaches.

2. Do some fancy stuff with routes and entry queries
I think technically you could set up a route like flowers/{productCategory}/{slug} and then in your template do an entry query for an entry with that slug. But probably better to think over #1 and find a cleaner solution.

  • No 2 is on the radar and yes it feels dirty :) Will revisit no 1 – joomkit Apr 29 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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