1

Channel: Restaurants

Channel: Nutrition / contains entries field named restaurant.

Required URL format: restaurant-name-nutrition

since the URL structure is merged I can't use getSegement(x) while getting entries.

in other scenario where the URL would be

nutrition/restaurant-name

I could use something like this

{% set seg2 = craft.request.getSegment(2)
{% for entry in craft.entries.section('nutrition').slug(seg2) %}

but what is the right approach for that one?

1

Why don't you use your current entry as reference?

{% set nutritions = craft.entries.section('nutrition').relatedTo({
    field: 'restaurant',
    targetElement: entry
}).all() %}

Since your current entry is related to the entry you want to fetch this would be the besser solution. However that URL structure is not really optimal to beginn with. There are many cases it will break as soon as someone edits your entries, but filtering by slug alone is never a good idea in such a way.

  • That's because there are no entries created with slugs like restaurant-name-nutrition. The /nutrition/index.twig template lists restaurants that have nutritions, urls are generated automatically - restaurant-name and nutrition postfix which are handled via route for proper template based on this url format. I'd strongly avoid creating categories or entries that would have restaurant-name-nutrition slugs. I know it would be easier but it adds too much complexity. – Mark H. Mar 12 at 22:45

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.