1

I have a site with projects which each has a projects structure with an associated category and a number of images.

The URL is set as projects/{projectCat.first.slug}/{slug}

I want to use the structure first for a set of thumbnails for each project (easy - no problem) and then for a gallery page with full size images. Both pages will have the same set of images. The URL for the first is .com/projects/category/project-name.

The only way I have been able to do it is to make the links from the thumbnail page using:

<a href="/projects/gallery/{{ category.slug }}/{{ entry.slug }}">

and then using routing:

/projects/gallery/slug/slug to projects/gallery/_entry

This works but if I have a project in two or more categories with the same name it doesn't take account of the category in the URL and is also a bit messy.

Is there a better way?

3
  • I'm having trouble understanding your issue – can you rephrase/be more specific? What exactly goes wrong if a project entry has "two or more categories with the same name"? Also, since you're using the category slug in the URLs, I don't really understand how multiple categories with the same name would even be an issue – the slug will always be unique (within the same Category Group), even if the name is identical, no? And since it's only possible to set a Categories Field to a single Category Group, identical names shouldn't be an issue. Commented Jul 26, 2018 at 12:56
  • Thanks @Mats - the problem is that my link is just a relative path with the category name followed by the project name, and the two are not related so Craft just uses the project name in isolation so the relation of the project to the category is not there. The actual ling from the structure URL woks fine, but not the hand done one Link i
    – Jerry
    Commented Jul 26, 2018 at 16:44
  • Thanks @Mats - the problem is that my link is just a relative path with the category name followed by the project name, and the two are not related so Craft just uses the project name in isolation so the relation of the project to the category is not there. The actual link from the structure URL woks fine, but not the hand coded one. I only have one entry type for the structure so don't seem to be able to use that. Site is link
    – Jerry
    Commented Jul 26, 2018 at 16:56

1 Answer 1

1

I finally managed to answer my own question. I changed my routing to

/projects/slug/slug/gallery to projects/gallery/_entry

So was able to use a proper absolute link with /gallery added to the end

.com/projects/category/project/gallery

In the gallery _entry I used the following code to get the entry

{% set projectSlug = craft.request.getSegment(-2) %} {% set categorySlug = craft.request.getSegment(-3) %} {% set category = craft.categories.slug(categorySlug).first() %}
{% set entry = craft.entries.section('projects').slug(projectSlug).relatedTo(category).first() %}

This then worked fine, even if the same named project was used in two different categories

I would still prefer to do this without routing if anyone has any suggestions please?

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.