2

my url looks like this:

domain.tld/slug-1/slug-2/variable

And in Twig it shall look for an entry with the slug slug-1 and show it, then an entry with the slug slug-2 and show it and look up for a global where the name of the global is variable.

How do I do this? I found:

{% set entry = craft.entries.section('section_handle').slug('my_slug') %}

But how do I tell Twig whats the first and whats the second slug?

There are always 2 slugs and one variable.

Thank you!

2

You can get the parts of your URL using the getSegment() function. https://craftcms.com/docs/templating/craft.request#getSegment

Then use the slug parameter and the criteria model’s first() method to grab the entry. Global Sets are already preloaded, to access a set dynamically grab it from the _context variable.

{% set entryA = craft.entries.slug(craft.request.getSegment(1)).first() %}
{% set entryB = craft.entries.slug(craft.request.getSegment(2)).first() %}
{% set globalSet = _context[craft.request.getSegment(3)] ?? null %}

{% if not entryA or not entryB or not globalSet %}
    {% exit 404 %}
{% endif %}

{{ entryA.title }} - {{ globalSet.myField }}
| improve this answer | |

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.