2

I have a main structure as my navigation. Within this structure I have multiple image galleries. I want to make the image gallery's urls dynamic.

If I define a gallery within the structure entry:
https://mysite.com/sport/gallery/

The url of the images in the gallery (which is a channel) should be:
https://mysite.com/sport/gallery/mygallerySlug?type=gallery&parent=parentEntryId

now I would like to have a route which catches all of the ?type=gallery entries and load the "gallery/_entry" template.

It shouldn't matter how deep in the structure the ?type=gallery is.

I don't seem to be able to do this within the CP, and I've never used advanced routing...

Any help appreciated!

EDIT:

Maybe I need to specify a little further:

I have a structure called "pages" which acts as the main structure.
I have a channel that is called "galleries" where I can upload images and assign a category. In my main structure I can assign galleries from within a matrix block and choose a category. That way I can regroup multiple entries from the channel "galleries" in a structure entry.

The urls of the galleries should be completely dynamic:

https://mysite.com/sport/gallery holds multiple gallery entries where each of these galleries should have a url the following way:
https://mysite.com/sport/gallery/gallery1
https://mysite.com/sport/gallery/gallery2 ...

another gallery collection could be here: https://mysite.com/karting/galleries/ whith links like that: https://mysite.com/karting/galleries/gallery3
https://mysite.com/karting/galleries/gallery4
etc..

I know I could specify the entry url of one gallery the following way in the "galleries" section url settings:

{{ craft.entries.id(65).locale('de').first.uri }}/{slug}
where the id is the id of e.g. https://mysite.com/sport/gallery/ structure entry.

But that works only for one gallery.

I could create a route for "sport/gallery/*" and assign the "gallery/_entry" template to it. But that way I would have to create a route for every gallery, which is not completely obvious, nor intuitive on a editor's point of view.

In fact this is almost a duplicate of this question:
dynamic urls for channel/structure that's linked to the main structure

Cheers
Stefan

  • You don't have to do this in the control panel. You could do it completely in the template. Why do you want to load a specific template based on the query string when you already have "gallery" in the url structure? Not trying to be mean genuinely curious about your use case! – Alan Miller Apr 12 at 21:55
  • problem is hitting that "gallery/_entry" template. If I don't define a route, it will generate a 404 page. – outline4 Apr 15 at 10:27
2

You can do this completely in the template. Have the structure entries resolve to a route like gallery/_router and then inside that template you could use this code to include the template of your choice:

{% switch craft.app.request.getParam('type') %}

    {% case "gallery" %}

        {% include "gallery/_entry" %}    

    {% case "anotherParam" %}

        {% include "gallery/_anotherParam" %}

     {% default %}

        {% include "gallery/_default" %}   

{% endswitch %}
  • Problem is that the page where the ?type parameter is, will generate a 404 error when you don't specify a route... I edited the question to better explain the situation... – outline4 Apr 15 at 10:28

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.