<p>You're not far away by combining what you already have with the example in the docs! You just need to pass in the category (<code>.relatedTo(category)</code>) and number of entries (<code>.limit(10)</code>) to show on each page.</p>
<pre><code>{% set query = craft.entries()
    .section('mySection')
    .relatedTo(category)
    .limit(10)
%}

{% paginate query as pageInfo, pageEntries %}

{% for entry in pageEntries %}
     &lt;p&gt;&lt;a href=&quot;{{ entry.url }}&quot;&gt;{{entry.title}}&lt;/a&gt;&lt;/p&gt;
{% endfor %}

{% if pageInfo.prevUrl %}&lt;a href=&quot;{{ pageInfo.prevUrl }}&quot;&gt;Previous Page&lt;/a&gt;{% endif %}
{% if pageInfo.nextUrl %}&lt;a href=&quot;{{ pageInfo.nextUrl }}&quot;&gt;Next Page&lt;/a&gt;{% endif %}
</code></pre>

CC BY-SA 4.0
2021-01-19T18:25:09.717Z
Thanks, but that doesn't work for me. It just limits the number of results returned from the Categories. In other words that .limit(10) only gives me ten results. I tried changing it to every thing from 1 to 25 and it always just limits what is returned to that limit number (total, not limited per page). What I need is every item, even if it's 200 of them, and then paginate that group to 20 per page, etc.
doctorgloom
CC BY-SA 4.0
2021-01-19T18:43:05.020Z
Never mind, it is working now. I retried it by copy and paste and got it working (probably a typo on my end). Thanks for the help!
doctorgloom
CC BY-SA 4.0
2021-01-19T22:29:37.757Z
Glad to hear you got it working!
revlis
CC BY-SA 4.0
2021-01-21T20:10:14.000Z
It works great, except it gives a Deprecation warning in the Admin panel. Apparently limit() is deprecated and you have to use all() now, but I don't understand how that would work with pagination in my case. It basically breaks this solution.
doctorgloom
CC BY-SA 4.0
2024-10-17T08:12:30.833Z
@revlis you save my day!
Tan Nguyen
CC BY-SA 4.0
2021-01-19T00:51:03.613Z
2021-01-19T00:51:03.613Z
revlis
<p>I'm trying to wrap my head around Pagination in Craft. I have a template that outputs a list of products based on the Category that is passed to it <a href="https://craftcms.stackexchange.com/questions/36985/simple-product-listing-pages-by-category-slug">from this thread:</a></p>
<pre><code>{# Fetch the entries related to auto-injected category variable #}
{% set entries = craft.entries()
    .section('mySection')
    .relatedTo(category)
    .all()%}

{% for entry in entries %}
    &lt;p&gt;&lt;a href=&quot;{{ entry.url }}&quot;&gt;{{entry.title}}&lt;/a&gt;&lt;/p&gt;
{% endfor %}
</code></pre>
<p>It works great, but I need to paginate it. I'm new to Craft so I'm sure I'm missing something simple, but all the examples I've seen, <a href="https://craftcms.com/docs/3.x/dev/tags.html#paginate" rel="nofollow noreferrer">plus the docs</a>, show how to paginate based on all the entries in a section, but that's doesn't work for me here. Also, the examples seem to conflict with my code, in so far as they are using 'set entries' to get the pagination working, but I'm using it already to get every item from a category. As I said, newbie here so I'm sure I'm missing something simple; any help appreciated.</p>
<p>I'm also hoping I can generate a numeric pagination, but at the moment I'd settle for understanding how to get the basics working in my category example.</p>

<categories><pagination>
Paginate based on items in a Category
# Paginate based on items in a Category

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

I'm trying to wrap my head around Pagination in Craft. I have a template that outputs a list of products based on the Category that is passed to it from this thread:
{# Fetch the entries related to auto-injected category variable #}
{% set entries = craft.entries()
    .section('mySection')
    .relatedTo(category)
    .all()%}

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

It works great, but I need to paginate it. I'm new to Craft so I'm sure I'm missing something simple, but all the examples I've seen, plus the docs, show how to paginate based on all the entries in a section, but that's doesn't work for me here. Also, the examples seem to conflict with my code, in so far as they are using 'set entries' to get the pagination working, but I'm using it already to get every item from a category. As I said, newbie here so I'm sure I'm missing something simple; any help appreciated.
I'm also hoping I can generate a numeric pagination, but at the moment I'd settle for understanding how to get the basics working in my category example.

**Tags:** categories, pagination


---

## 1 answers

### Answer by username_1 on 2021-01-19. Score: 2 | Accepted answer

You're not far away by combining what you already have with the example in the docs! You just need to pass in the category (.relatedTo(category)) and number of entries (.limit(10)) to show on each page.
{% set query = craft.entries()
    .section('mySection')
    .relatedTo(category)
    .limit(10)
%}

{% paginate query as pageInfo, pageEntries %}

{% for entry in pageEntries %}
     {{entry.title}}
{% endfor %}

{% if pageInfo.prevUrl %}Previous Page{% endif %}
{% if pageInfo.nextUrl %}Next Page{% endif %}

> **Comments:**
> **username_0**: Thanks, but that doesn't work for me. It just limits the number of results returned from the Categories. In other words that .limit(10) only gives me ten results. I tried changing it to every thing from 1 to 25 and it always just limits what is returned to that limit number (total, not limited per page). What I need is every item, even if it's 200 of them, and then paginate that group to 20 per page, etc.  
> **username_0**: Never mind, it is working now. I retried it by copy and paste and got it working (probably a typo on my end). Thanks for the help!  
> **username_1**: Glad to hear you got it working!  
> **username_0**: It works great, except it gives a Deprecation warning in the Admin panel. Apparently limit() is deprecated and you have to use all() now, but I don't understand how that would work with pagination in my case. It basically breaks this solution.  
> **username_2**: @username_1 you save my day!  

---

