2

I've asked already about nested categories here, but I've just upgraded to Craft 3 and things seem to have changed a little.

I have three top-level category groups:

- Home
- About
- Funding

Each of these has multiple categories, and some of these categories are nested up to three levels deep.

My problem: the nested category URIs don't include the category group (which I appreciate doesn't have its own slug). Instead I get stuff like this:

/cat-A/subcat-1/
/cat-B/subcat-2/subcat-6/

... when what I want is:

about/cat-A/subcat-1/
funding/cat-B/subcat-2/subcat-6/

If I manually add funding/ or about/ as part of those categories' URI formats (eg. funding/{parent.uri}/{slug}) then it appends funding/ at each level, eg: funding/funding/funding/cat-B/subcat-2/subcat-6.

I've managed to achieve what I want for entries, by setting their URI format like so:

{category.first().group.handle}/{category.last().uri}/{parent.uri}/{slug}

... which gives me stuff like:

funding/cat-B/subcat-2/subcat-6/entry-title-slug

I only really need this feature to work because I envision users browsing the categories and potentially clicking the URI/links and being taken to the wrong place.

In my earlier post I created a top-level category group called "Navigation" which contained all my categories – this achieved the correct URIs. This feels messy/hacky though and means my users have to see a redundant "Navigation" group when editing categories.

It's likely I'm misunderstanding some aspect of Craft here – can anyone help me achieve what I'm trying to do here?

1
  • I can't tell... is this specific to Craft 3 or do you have the same issue on Craft 2?
    – Brad Bell
    Sep 14 '17 at 15:43
1

I'm not sure if this is a Craft 3 only issue. The following should work on Craft 2:

Top-level Category: something/{slug}

Nested Category: {parent.uri}/{slug}

1
  • Thanks – I ended up making a top-level "Navigation" category group and then added all my categories under that. I then used a section with this Entry URI format: {category.last().uri}/{parent.uri}/{slug}. Sep 15 '17 at 13:02
2

You would just enter { parent? parent.slug : '' }/{ slug } in the "Category URI Format" Field in the Craft3 CP and it will recognize nested categories

2

You can use Twig logic in you Category URL Format like this:

{parent ? parent.uri : group.handle }/{slug}

This will prepend the category group's handle (main, about, funding, etc.) if it's a top-level category (no parent). Otherwise it prepends the parent category's full URI.

Given you have a Category Group called "About" with the handle "about," and that group has nested categories like this:

|-- cat-1
|   |-- cat-1-1
|   |-- cat-1-2
|       |-- cat-1-2-1
|
|-- cat-2

You'll get URIs like this:

/about/cat-1
/about/cat-1/cat-1-1
/about/cat-1/cat-1-2
/about/cat-1/cat-1-2/cat-1-2-1
/about/cat-2

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .