<p>You don't actually need to look at the URL segments, because Craft automatically populates your template with a <code>category</code> variable whenever it matches the current URL to a category's URL. All you need to do, is to make sure that your category URLs point to the correct template, and to account for that template being rendered both with a <code>category</code> variable and without (i.e. for the default news index listing).</p>

<p>First, you'll need to make sure that the URL format for your category group is correct – you should probably use something like <code>news/{slug}</code> or perhaps <code>news/categories/{slug}</code>. Also, set the "Category template" to your news index template, e.g. <code>news</code> or whatever template path you're actually using for the news index.</p>

<p>Then, you can filter your entries like the following, using the <a href="https://craftcms.com/docs/relations#the-relatedTo-param" rel="nofollow noreferrer"><code>relatedTo</code></a> parameter:</p>

<pre><code>{% set category = category ?? null %}
{% paginate craft.entries.section('news').featured('not 1').relatedTo(category).limit(6) as pageInfo, pageEntries %}

...
</code></pre>

<p>One gotcha to note is that whenever you pass a <code>NULL</code> value to the <code>relatedTo()</code> parameter (which will happen whenever the <code>category</code> variable is empty), Craft will ignore the parameter and return all entries – which works in this case, as the same template should also double as your news index.</p>

CC BY-SA 3.0
2016-11-15T15:00:16.390Z
ah ok thanks. Ya I want it to default to show all so that works. The only issue I'm having now is when I drop in the category navigation it seems to load the site index page rather than the news index page and the articles are not listed there. I've edited the original post to show my navigation as well.
Matthew Carleton
CC BY-SA 3.0
2016-11-15T15:02:53.077Z
How do you build the "category navigation" currently? Sounds like your hrefs are simply off a bit.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-11-15T15:05:33.150Z
ya you are probably right. See my edit at the bottom.
Matthew Carleton
CC BY-SA 3.0
2016-11-15T15:35:58.097Z
ok that does. Now the only thing is how to remove the featured post if it doesn't fall within that category. Right now if I choose any category it shows the featured post.
Matthew Carleton
CC BY-SA 3.0
2016-11-15T15:42:41.637Z
ah, nevermind adding .relatedTo(category) to the featuredEntry did the trick. Thanks!
Matthew Carleton
CC BY-SA 3.0
2016-11-15T12:36:51.467Z
2016-11-15T15:19:44.900Z
2016-11-15T15:19:44.900Z
Mats Mikkel Rummelhoff
<p>I've got my news entries paginated and I have a featured entry on the first page so I'm wondering what the best approach would be to filter by category. Here is what my template looks like:</p>

<pre><code>{% extends "_layout" %}
{% set title = "News" %}

{% paginate craft.entries.section('news').featured('not 1').limit(6) as pageInfo, pageEntries %}
{% set segment = craft.request.segment(2) %}
{% set pageCats = craft.categories('newsCategories').slug('segment') %}
 {% block content %}
&lt;section class="news-page section-wrap news-list"&gt;
    &lt;h1&gt;Company News&lt;/h1&gt;
    {% if pageInfo.currentPage == 1 %}       
    {% for featuredEntry in craft.entries.section('news').featured('1') %}
        &lt;article class="news-list--item news-list--feature"&gt;
            &lt;div class="feature-img"&gt;
                {% for image in featuredEntry.featuredImage %}
                    &lt;img src="{{ image.getUrl('featuredArticleImage') }}" alt="{{ featuredEntry.title }}"/&gt;
                {% endfor %}
            &lt;/div&gt;
            &lt;div class="feature-content"&gt;
                &lt;h1&gt;&lt;a href="{{ featuredEntry.url }}"&gt;{{ featuredEntry.title }}&lt;/a&gt;&lt;/h1&gt;
                {{ featuredEntry.body.getPage(1) }}
                &lt;a class="btn btn-secondary" href="{{ featuredEntry.url }}"&gt;Continue reading&lt;/a&gt;
            &lt;/div&gt;
        &lt;/article&gt;
    {% endfor %}
    {% endif %}
    {% for regularEntry in pageEntries %}
        &lt;article class="news-list--item"&gt;
            &lt;a href="{{ regularEntry.url }}"&gt;
                &lt;h2&gt;{{ regularEntry.title }}&lt;/h2&gt;
                {{ regularEntry.body|hacksaw(words='25', allow='&lt;p&gt;&lt;b&gt;', append='...') }}
                &lt;span class="cta-border"&gt;Continue reading&lt;/span&gt;
            &lt;/a&gt;
        &lt;/article&gt;
    {% endfor %}

&lt;/section&gt;
&lt;nav class="pagination"&gt;
    {% if pageInfo.prevUrl %}&lt;a href="{{ pageInfo.prevUrl }}"&gt;Previous Page&lt;/a&gt;{% endif %}
    {% if pageInfo.nextUrl %}&lt;a href="{{ pageInfo.nextUrl }}"&gt;Next Page&lt;/a&gt;{% endif %}
&lt;/nav&gt;
{% endblock %}

{% nav category in craft.categories.group('news') %}
&lt;li&gt;&lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;&lt;/li&gt;
{% endnav %}
</code></pre>

<categories><pagination>
filtering entries by category
# filtering entries by category

(Asked by: username_0 on 2016-11-14)

I've got my news entries paginated and I have a featured entry on the first page so I'm wondering what the best approach would be to filter by category. Here is what my template looks like:
{% extends "_layout" %}
{% set title = "News" %}

{% paginate craft.entries.section('news').featured('not 1').limit(6) as pageInfo, pageEntries %}
{% set segment = craft.request.segment(2) %}
{% set pageCats = craft.categories('newsCategories').slug('segment') %}
 {% block content %}

    Company News
    {% if pageInfo.currentPage == 1 %}       
    {% for featuredEntry in craft.entries.section('news').featured('1') %}
        
            
                {% for image in featuredEntry.featuredImage %}
                    
                {% endfor %}
            
            
                {{ featuredEntry.title }}
                {{ featuredEntry.body.getPage(1) }}
                Continue reading
            
        
    {% endfor %}
    {% endif %}
    {% for regularEntry in pageEntries %}
        
            
                {{ regularEntry.title }}
                {{ regularEntry.body|hacksaw(words='25', allow='', append='...') }}
                Continue reading
            
        
    {% endfor %}



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

{% endblock %}

{% nav category in craft.categories.group('news') %}
{{ category.title }}
{% endnav %}

**Tags:** categories, pagination


---

## 1 answers

### Answer by username_1 on 2016-11-15. Score: 2 | Accepted answer

You don't actually need to look at the URL segments, because Craft automatically populates your template with a category variable whenever it matches the current URL to a category's URL. All you need to do, is to make sure that your category URLs point to the correct template, and to account for that template being rendered both with a category variable and without (i.e. for the default news index listing).
First, you'll need to make sure that the URL format for your category group is correct – you should probably use something like news/{slug} or perhaps news/categories/{slug}. Also, set the "Category template" to your news index template, e.g. news or whatever template path you're actually using for the news index.
Then, you can filter your entries like the following, using the relatedTo parameter:
{% set category = category ?? null %}
{% paginate craft.entries.section('news').featured('not 1').relatedTo(category).limit(6) as pageInfo, pageEntries %}

...

One gotcha to note is that whenever you pass a NULL value to the relatedTo() parameter (which will happen whenever the category variable is empty), Craft will ignore the parameter and return all entries – which works in this case, as the same template should also double as your news index.

> **Comments:**
> **username_0**: ah ok thanks. Ya I want it to default to show all so that works. The only issue I'm having now is when I drop in the category navigation it seems to load the site index page rather than the news index page and the articles are not listed there. I've edited the original post to show my navigation as well.  
> **username_1**: How do you build the "category navigation" currently? Sounds like your hrefs are simply off a bit.  
> **username_0**: ya you are probably right. See my edit at the bottom.  
> **username_0**: ok that does. Now the only thing is how to remove the featured post if it doesn't fall within that category. Right now if I choose any category it shows the featured post.  
> **username_0**: ah, nevermind adding .relatedTo(category) to the featuredEntry did the trick. Thanks!  

---

