11

In Craft 2.x you have the option of setting the top-level entries URL format for a structure section separately from nested entries i.e. top-level/{slug} and {parent.uri}/{slug}

But in Craft 3 the same URL format is applied for all entries within the structure section.

This means if you use top-level/{parent.uri}/{slug} it works fine for a top-level entry but then nested entries have the first segment duplicated, e.g. top-level/top-level/some-parent/the-slug. How does one achieve the same level of URL formatting for structure entries in Craft 3?

1

1 Answer 1

20

To pull off the same functionality in Craft 3, you could use something like this:

{entry.last().uri}/{parent.uri}/{slug}
6
  • 3
    Awesome, thanks Brad. Might be worth somehow including this in the CP field instructions for a structure or elsewhere in the craft doco as I would assume most people would want this format for a structure section. :) Commented Jan 26, 2018 at 2:08
  • 7
    I ended up using the following: {% if level == 1 %}{section.handle}{% else %}{parent.uri}{% endif %}/{slug} Commented Jan 28, 2018 at 7:23
  • 6
    the accepted answer turned out faulty on my craft 3 version. I ended up using {parent.uri ?? 'top-level'}/{slug} and solved my issue of duplicated segments while allowing for a fixed segment. The solution of Rich could be rewritten as {parent.uri ?? section.handle}/{slug} for readability purposes
    – 3xil3
    Commented Jan 29, 2019 at 22:59
  • 3xil3 has the correct answer.
    – biko
    Commented Apr 4, 2019 at 4:14
  • After trying all answers here and at the related question here: craftcms.stackexchange.com/q/26483/4060 , the only answer that seems to allow for a static, non-duplicated segment in the url is here: craftcms.stackexchange.com/a/26484/4060
    – Tchick
    Commented Sep 22, 2019 at 15:22

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.