2

I am trying to set up the URIs for some nested categories in Craft 3 but am having some issues. My structure is basically:

service-one
|-subservice-one-a
|-subservice-one-b
service-two
|-subservice-two-a
|-subservice-two-b

I have set my Category URI Format to:

what-we-do/{slug}

Which is working fine, however I would like the URI for the nested categories to look like:

what-we-do/service-one/subservice-one-a

rather than what I have currently:

what-we-do/subservice-one-a

Is this possible?

2 Answers 2

10

You can actually use a bit of Twig logic to determine your category URI formats.

Try this for your Category URI Format:

{parent ? parent.uri : 'what-we-do'}/{slug}

Which is just a more compact way of doing this:

{% if parent %}{{ parent.uri }}{% else %}what-we-do{% endif %}/{slug}

That will only add 'what-we-do' to the URI if the current category doesn't have a parent (top-level category). Otherwise, it prepends the entire parent's URI.

That should give you URIs like:

/what-we-do/service-one
/what-we-do/service-one/subservice-one-a
/what-we-do/service-one/subservice-one-b
/what-we-do/service-one/subservice-one-b/subservice-one-b-nested
2

You should be able to set your Category URI Format to {parent.slug}/{slug}

If they are on the parent it will only show the parent category. If they are on the child it will show both.

So you'd set it to

what-we-do/{parent.slug}/{slug}

There is also this answer if you need the path beyond 1 level:

{parent.uri}/{slug}

1
  • If you do that Darryl you end up with tld.com/what-we-do/what-we-do/category-parent/category-child. Commented Oct 11, 2019 at 18:14

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.