3

I have a single category field (named category) in my section and I am looking to use the auto-generated title to keep these entries titled consistently. I have had success outputting the first category selection into the entry field but I can't seem to grab multiple selections. I have tried a for loop and I can of course getting working on a template but I am thinking the syntax must be different for the auto generated title field.

Here is an example of the tree I am working with.

  • Category One
    • Sub Cat One
    • Sub Cat Two

In a example entry I will have Sub Cat One selected and of course Category One since it is an ancestor of the Sub Cat One. I would like to output something like (Category One / Sub Cat One) in the entry title but I either get no results or just the first one. I have tried the following code:

{% for categoryList in category %}{{categoryList.title}}{% if loop.index < loop.length %}/{% endif %}{% endfor %}

and

{category.first().title}

Which of course contains only the first selected category title.

How do you output more than one category selection in an entry title field?

Thanks!

2 Answers 2

2

When you use the single brace tag syntax within your URL Format field:

{category.first().title}

that’s actually a shortcut for this:

{{ object.category.first().title }}

So, object is the actual variable name that you can use to refer to your entry, when you want to use the normal Twig tags {{ ... }} and {% ... %}.

{% for categoryList in object.category %}{{ categoryList.title }}{% if not loop.last %}/{% endif %}{% endfor %}
1
  • It works of course!! Awesome and good to know. Commented Sep 11, 2014 at 17:37
3

I think you could also use Twig's join filter instead of the for loop:

{{ object.category | join('/') }}
0

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.