0

I have a Category Group that can be 3 levels deep. Each category has its own page and its URI structure appends each category as you go deeper, like so:

{parent ? parent.uri : 'root-uri' }/{slug}

Which produces:

/root-uri/category-1/category-2/category-3

This all works fine.

I then have a field to assign these categories to an entry. My goal is to create a URI format for the entries that include the hierarchy of the categories it's assigned to. Like:

/root-uri/category-1/category-2/category-3/entry-slug

or

/root-uri/category-1/entry-slug

What is the proper URI format for the entries to accomplish this?

Update

Using {categoriesField.last().uri}/{slug} is working, but the .last() function is deprecated.

2 Answers 2

1

The URL format can be a full-blown twig template, so you can use a regular category query to retrieve the category. To replace the deprecated last() method, you can use leaves() to narrow the category query to only return leaf nodes (categories with no descendants), then use one() to return the first matching result:

{{ object.categoriesField.leaves().one().uri ?? '' }}/{{ object.slug }} 
1

Since last() is deprecated, here is the updated version that works:

{categoriesField.inReverse().one().uri}/{slug}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.