1

I'm familiar with the option in the category settings that allows for categories within a category group to each have their own URLs: enter image description here

I have this setup and it is working fine. However, I'm wondering if there is a URL structure that will allow me to have a category group "home" URL in which entries from all categories within the category group are shown.

I know there are a couple of different ways I could do this by setting up templates for each category group "home" page, but I would prefer a way to do this using a single template that took into account the URL structure.

0

1 Answer 1

4

Create a URL structure like this:

yourwebsite.com/categories/groupName

Then in your template:

{% set group = craft.request.getSegment(2) %}

{% set categories = craft.categories.group(segment) %}

Once you have the categories you can loop over them and fetch entries related to each. It may however, be more beneficial to do this via a plug-in where you can construct a more complex query based on the categories and therefore be more efficient.

{% for category in categories %}

    {% set entries = craft.entries.relatedTo(category) %}

    {% for entry in entries %}
        // template stuff
    {% endfor %}

{% endfor %}
5
  • In my situation I have a multi-locale site, so I don't think this will work. In some languages the category group segment would be different than the category group handle. However, this does seem like a possible solution for a monolingual site. I suppose I should have mentioned that I was working on a multi-locale site in my original question.
    – Justin K
    Commented May 1, 2016 at 19:49
  • Can you create a language agnostic groupHandle and work off that? Then create a secondary field that can be translated with respect to the locale and displayed in the DOM as a header or something
    – mcclaskiem
    Commented May 1, 2016 at 20:11
  • The problem I'm seeing is that the URL segment will change depending on the language so I cannot use it as a way to set the group variable in my template reliably. That is if I'm understanding you correctly and understanding your code above. My solution was to use several single pages for the "category home" pages. It is kind of annoying to have those single pages but it works well because singles have good multi-locale support.
    – Justin K
    Commented May 1, 2016 at 20:58
  • Can you use a twig macro to translate the URL into whatever language the category title is?
    – mcclaskiem
    Commented May 1, 2016 at 21:17
  • 1
    Yeah, that would probably work. Again, setting up a few singles pages just seems like a more straight forward solution to me. Thanks again for all your ideas @mcclaskiem.
    – Justin K
    Commented May 2, 2016 at 18:07

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.