<p>You're nearly there! You are missing the part that fetches the entries related to your category.</p>
<p>With your current set up, you would need to do:</p>
<pre><code>{# Get the requested category slug from the URL #}
{% set requestedSlug = craft.app.request.segments|last %}


{# Fetch the category with that slug #}
{% set category = craft.categories()
    .slug(requestedSlug|literal)
    .one() %}

{# Fetch the entries related to your category #}
{% set entries = craft.entries()
    .section('yourSectionHandle')
    .relatedTo(category)
    .all()%}

{% for entry in entries %}
    {{entry.title}}
    {{entry.url}}
    {{entry.image}}
{% endfor %}
</code></pre>
<p>Replace <code>yourSectionHandle</code> with, well... your section handle :)</p>
<p>Alternatively, Craft provides a way to simplify the process a bit.</p>
<blockquote>
<p>First, it doesn't look to me like you can create separate templates for each category, only the Category group, correct?</p>
</blockquote>
<p>You can manually create a template for each category by making a file named as your category slug but this is tedious to maintain.</p>
<p>The way you could handle that would start when you define your category group's settings.</p>
<p>This is only an example but you should get the gist of it.</p>
<p>First, define Settings → Categories → Your Group → <strong>Category URI Format</strong> to be <code>categories/{slug}</code>, the <strong>Template</strong> to <code>categories</code> and make a <code>categories.twig</code> template at the root of your <code>templates</code> folder.</p>
<p>Now, when you access <code>yoursite.com/categories/category-slug</code> (<code>category-slug</code> being an actual category slug), Craft will auto-inject a <code>category</code> variable to the template (like the <code>entry</code> variable on entry pages) allowing you to only have the following in the template:</p>
<pre><code>{# Fetch the entries related to your auto-injected category variable #}
{% set entries = craft.entries()
    .section('yourSectionHandle')
    .relatedTo(category)
    .all()%}

{% for entry in entries %}
    {{entry.title}}
    {{entry.url}}
    {{entry.image}}
{% endfor %}
</code></pre>
<p>Hopefully, that makes sense for you!</p>

CC BY-SA 4.0
2021-01-15T19:37:13.547Z
Thank you so much for the detailed reply, I really appreciate it. I won't be able to try this until the weekend but I'll post back once I do. Thanks again.
doctorgloom
CC BY-SA 4.0
2021-01-15T20:21:09.537Z
I did get a minute to try this, but unfortunately both ways throw an error for me. From the debugger:  *Twig\Error\SyntaxError: Unknown "entries" tag.*
doctorgloom
CC BY-SA 4.0
2021-01-15T20:30:05.890Z
Okay I figured out the problem. I copied and pasted your code and didn't noticed you forgot *set* in the second line. So thanks again for the help!
doctorgloom
CC BY-SA 4.0
2021-01-16T06:57:39.393Z
Ooops...! I have corrected my answer.
Oli
CC BY-SA 4.0
2021-01-14T07:09:38.830Z
2021-01-16T06:57:15.123Z
2021-01-16T06:57:15.123Z
Oli
<p>I'm new to Craft and I'm trying to create very basic product listing pages based on Categories and I'm not sure where to start.</p>
<p>First, it doesn't look to me like you can create separate templates for each category, only the Category group, correct?</p>
<p>If that's true, then I need that template to be dynamic and list the products in a category that may be nested. The idea being that I will have navigation that will resolve to each category page.</p>
<p>I've gone through the page on <a href="https://craftcms.com/docs/3.x/categories.html#querying-categories" rel="nofollow noreferrer">Querying Categories</a> and I <em>think</em> querying by Slug is what I want. I altered the code on that page based on <a href="https://craftcms.stackexchange.com/questions/27671/setting-up-multiple-category-listing-pages-on-site-with-multiple-locales?">this post</a> in order to get whatever the last segment is:</p>
<pre><code>{# Get the requested category slug from the URL #}
{% set requestedSlug = craft.app.request.segments|last %}


{# Fetch the category with that slug #}
{% set category = craft.categories()
    .slug(requestedSlug|literal)
    .one() %}

{% set entry = category %}

{% for entry in entries %}
    {{entry.title}}
    {{entry.url}}
    {{entry.image}}
{% endfor %}
</code></pre>
<p>For clarity I didn't put the entry items in HTML.</p>
<p>I feel like my problem is the second bit, setting the entry to category (which I cobbled together from posts here and in the docs) but I can't figure out how to <em>get at</em> the entry details from items in a category.</p>
<p>I also need to paginate the listings if that makes a difference.</p>
<p>Thanks in advance for any advice or pointers to good tutorials on using Categories to generate product listings.</p>

<templating><categories><structure>
Simple Product Listing Pages by Category Slug
# Simple Product Listing Pages by Category Slug

(Asked by: username_0 on 2021-01-13)

I'm new to Craft and I'm trying to create very basic product listing pages based on Categories and I'm not sure where to start.
First, it doesn't look to me like you can create separate templates for each category, only the Category group, correct?
If that's true, then I need that template to be dynamic and list the products in a category that may be nested. The idea being that I will have navigation that will resolve to each category page.
I've gone through the page on Querying Categories and I think querying by Slug is what I want. I altered the code on that page based on this post in order to get whatever the last segment is:
{# Get the requested category slug from the URL #}
{% set requestedSlug = craft.app.request.segments|last %}


{# Fetch the category with that slug #}
{% set category = craft.categories()
    .slug(requestedSlug|literal)
    .one() %}

{% set entry = category %}

{% for entry in entries %}
    {{entry.title}}
    {{entry.url}}
    {{entry.image}}
{% endfor %}

For clarity I didn't put the entry items in HTML.
I feel like my problem is the second bit, setting the entry to category (which I cobbled together from posts here and in the docs) but I can't figure out how to get at the entry details from items in a category.
I also need to paginate the listings if that makes a difference.
Thanks in advance for any advice or pointers to good tutorials on using Categories to generate product listings.

**Tags:** templating, categories, structure


---

## 1 answers

### Answer by username_1 on 2021-01-14. Score: 3 | Accepted answer

You're nearly there! You are missing the part that fetches the entries related to your category.
With your current set up, you would need to do:
{# Get the requested category slug from the URL #}
{% set requestedSlug = craft.app.request.segments|last %}


{# Fetch the category with that slug #}
{% set category = craft.categories()
    .slug(requestedSlug|literal)
    .one() %}

{# Fetch the entries related to your category #}
{% set entries = craft.entries()
    .section('yourSectionHandle')
    .relatedTo(category)
    .all()%}

{% for entry in entries %}
    {{entry.title}}
    {{entry.url}}
    {{entry.image}}
{% endfor %}

Replace yourSectionHandle with, well... your section handle :)
Alternatively, Craft provides a way to simplify the process a bit.

First, it doesn't look to me like you can create separate templates for each category, only the Category group, correct?

You can manually create a template for each category by making a file named as your category slug but this is tedious to maintain.
The way you could handle that would start when you define your category group's settings.
This is only an example but you should get the gist of it.
First, define Settings → Categories → Your Group → Category URI Format to be categories/{slug}, the Template to categories and make a categories.twig template at the root of your templates folder.
Now, when you access yoursite.com/categories/category-slug (category-slug being an actual category slug), Craft will auto-inject a category variable to the template (like the entry variable on entry pages) allowing you to only have the following in the template:
{# Fetch the entries related to your auto-injected category variable #}
{% set entries = craft.entries()
    .section('yourSectionHandle')
    .relatedTo(category)
    .all()%}

{% for entry in entries %}
    {{entry.title}}
    {{entry.url}}
    {{entry.image}}
{% endfor %}

Hopefully, that makes sense for you!

> **Comments:**
> **username_0**: Thank you so much for the detailed reply, I really appreciate it. I won't be able to try this until the weekend but I'll post back once I do. Thanks again.  
> **username_0**: I did get a minute to try this, but unfortunately both ways throw an error for me. From the debugger:  *Twig\Error\SyntaxError: Unknown "entries" tag.*  
> **username_0**: Okay I figured out the problem. I copied and pasted your code and didn't noticed you forgot *set* in the second line. So thanks again for the help!  
> **username_1**: Ooops...! I have corrected my answer.  

---

