6

I have a Structure section and a Channel section.

Entries in the Channel will be listed on one of the Structure Entries, and it's entry page will fit the same URL format.

So for example, the Structure Entry may be:

Title: "Our People"
URL: /about/our-people"

...and the Channel entry may be:

Title: "Jamie"
URL: /about/our-people/{slug}

When I set up the Channel is there any way of dynamically setting it's "Entry URL Format" based on another entry's URL/slug? I can set it to "/about/our-people/{slug}" but if the Structure entry's slug ever changes it'll be wrong.

I've tried adding the following to the Channel's Entry URL Format field and it seems to work (high fives P&T).

{% set parent = craft.entries.section('site').id('32').first() %}{{ parent.uri }}/{slug}

Is there anything I should be worried about using that? Obviously if that parent entry is deleted things will break. Anything else?

2
  • If the Structure parent entry is ever changed, or it's slug is changed it looks like all the entries in the Channel section would need to be resaved individually to pick up the change. Which isn't ideal. Jul 30, 2014 at 16:16
  • /about/our-people/{slug} would still work after you change the structure's slug to, let's say /about/team. Only thing that changes is, yep, the url to the structure entry. Just don't link to your entries with relative URLs (you probably wouldn't anyways, just saying).
    – carlcs
    Jul 31, 2014 at 15:58

1 Answer 1

8

I've successfully used this entry url format and it works great:

{{ craft.entries.id(65).first.uri }}/{slug}

I would advise using the entry id over a slug, as it will never change. You also don't need to specify the section if you've included the id.

For a multi-lingual site, you can specify the locale for each entry url format:

{{ craft.entries.id(65).locale('en_us').first.uri }}/{slug}
{{ craft.entries.id(65).locale('de').first.uri }}/{slug}
{{ craft.entries.id(65).locale('fr').first.uri }}/{slug}

As you mention in your comment, you do need to beware of updating the parent entry's slug as uri's are parsed before being stored in the database so it is not truly dynamic. I'm not aware of any workarounds for this that don't involve a plugin.

1
  • Thanks this helped me. The double curlies were key. Here's a more modern craft 4 syntax (I used entry type in the query to make it a hair more dynamic): {{ craft.entries().type('departmentListing').one().uri }}/{slug}
    – Brimby
    Oct 23, 2023 at 21: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.