2

I have a structure section, where the last child should be an anchor in the slug:

parent/parent#child

I'm trying to setup conditional url formats on nested entries as such:

{parent.uri}{% if object.getDescendants().total() != 0 %)/{% else %}#{% endif %}{slug}

I've also tried the simpler

{parent.uri}{% if object.hasDescendants %)/{% else %}#{% endif %}{slug}

but keep getting stuck tasks when re-saving entries. Can someone tell me what I'm doing wrong? Thanks in advance

  • What error is causing the tasks to get stuck? It'll be logged in craft/storage/runtime/logs. – Brad Bell Dec 8 '17 at 17:43
  • there's a lot in the craft.log, something like this? [error] [application] Encountered an error running task 18 (ResaveElements), step 2 of 39: An exception was thrown while trying to save the Entry with the ID “19”: Unexpected ")" in "{{object.parent.uri|raw}}{% if object.getDescendants().total() != 0 %)/{% else %}#{% endif %}{{object.slug|raw}}" at line 1. – Paul Dec 9 '17 at 2:29
  • Argh! I see my mistake right after I press submit. Fixed the typo and now all is good. Not sure how this has evaded me for so long yesterday... – Paul Dec 9 '17 at 2:35
2

There was a typo hidden in there, otherwise the logic works. To get children as anchors in a structure, the following should do the trick:

{parent.uri}{% if object.getDescendants().total() != 0 %}/{% else %}#{% endif %}{slug}

| improve this answer | |
  • There's also the slightly less verbose {parent.uri}{{ object.getDescendants().total() != 0 ? '/' : '#' }}{slug} – Paul Dec 9 '17 at 3:11

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.