2

I have a selection of category groups as follows:

Suitability
Colour
Finish
etc

They all have many categories within them. I have set up some routes for /suitability etc to show the user a /shop/category.twig template for the category group landing pages.

But my issue I'm having is getting the context of the page based on the slug.

I have read a very old SE post that kind of offers a solution, but it's only useful for getting the name. I need to build on this to get the group. But it doesn't look like there's a way to 'get' a category group.

I may be wrong and missed something but I've only found ways to obtain category group info based on context of a category itself as follows:

{# get the category group slug #}
{% set slug = craft.app.request.segment(1) %}

{# get the first category within this group and output the groups' name #}
{% set catGroupName = craft.categories.group(slug).first.group %}

Any advice on a much neater way to obtain a category group would be handy. Thanks

3

2nd Revision:

The category group field layout is for categories within that group. Categories groups do not have fields assigned to them, only to the categories within a group.


1st Revision:

To get the fields of a category group, set the category like so:

{% set group = craft.categories.group('suitability').one() %}

And use fields:

{{ group.someFieldHandle }}

Original Answer:

To retrieve the category:

{% set slug = craft.app.request.segment(1) %}
{% set category = craft.categories.group('suitability').slug(slug).one() %}

Then use its fields or related:

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

It would be better to set the template for the category on the category group settings page.

Set the URI to /suitability/{{ slug }} and the template to /shop/category.twig Then you can just use the category in the template without having to set it by getting slug and pulling the category from the group. Also, this allows you to point multiple category groups to the same template if their template will be similar.

  • Ah, see don't want the category. I want the category group and to be able to access the group's data like: {{ group.title }}, {{ group.description }} – Jay Dec 18 '18 at 18:53
  • Check revised answer. – Indigo Viking Dec 18 '18 at 18:58
  • I've tried that before unfortunately. And that's just getting the first category within that category group not the group itself. – Jay Dec 18 '18 at 19:01
  • Ah so how are you setting fields on the group itself? – Indigo Viking Dec 18 '18 at 19:02
  • I'm setting the fields for the group in the CMS within a selected category group's field layout tab. But I'm not sure how to get the context of the group and not of a category within the said group. – Jay Dec 18 '18 at 19:03

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.