I have a structure where the Entry URI Format is {parent.uri}/{slug} - what I'm looking to do is conditionally not include entry slugs within the uri based on a show/hide setting in the entry.

For example, I might have structure where an entry, sub-page2 has a uri like so page/sub-page/sub-page2 but if there is a setting on sub-page to exclude it, it's uri should be page/sub-page2

This would mean that the conditional to show it should only be considered if it's being called as a parent. sub-page should still be accessible from page/sub-page - trying to find a way to achieve this?


You have the full power of Twig in your template to render the entries Uri so you can conditionally render what you want. This could be your template in your section settings

{% if object.parent.field == true %}{{ object.parent.uri }}{% else %}{{object.parent.slug}}{% endif %}

There are no real limitations since you can do what you are used to in Twig in your frontend

  • Thanks for that. I feel like I'm close but not quite there. {% if object.parent.excludeFromUri %}{{ object.parent.slug }}{% else %}{{object.parent.uri}}{% endif %}/{slug} - which doesn't quite do what I'm after as in the first condition, (where the exclude is true) I need to get the objects parent parents uri.
    – shorn
    Mar 6 '19 at 22:25
  • Then add another parent to your output but check if there is a parent. You might want to do the following: create your well formatted code in your entries Twig template and set {% set object = entry %} and when you have the correct format you copy it in your cp. Mar 6 '19 at 22:28
  • Ok, Got it working, not sure it's the best to be doing it as it only allows up to 4 levels, so it might be worth rewriting to enable it to handle any level, but I'm pretty sure 4 levels is more than enough for my needs. {{ object.parent.parent.parent.excludeFromUri == 0 ? object.parent.parent.parent.slug~'/' }}{{ object.parent.parent.excludeFromUri == 0 ? object.parent.parent.slug~'/' }}{{ object.parent.excludeFromUri == 0 ? object.parent.slug~'/' }}{{ object.slug }} - bit of beast!
    – shorn
    Mar 6 '19 at 22:48
  • You might want to think about coding a little bit more defensive cause I guess most of your entries can't be resaved because of the exceptions that are thrown Mar 6 '19 at 22:50
  • Yea, just wanted to test it's possible then optimise.
    – shorn
    Mar 6 '19 at 22:53

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.