Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-15T15:00:16.390Z
Answers.Text: 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.
Answers.UserDisplayName: Matthew Carleton
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-15T15:02:53.077Z
Answers.Text: How do you build the "category navigation" currently? Sounds like your hrefs are simply off a bit.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-15T15:05:33.150Z
Answers.Text: ya you are probably right. See my edit at the bottom.
Answers.UserDisplayName: Matthew Carleton
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-15T15:35:58.097Z
Answers.Text: 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.
Answers.UserDisplayName: Matthew Carleton
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-15T15:42:41.637Z
Answers.Text: ah, nevermind adding .relatedTo(category) to the featuredEntry did the trick. Thanks!
Answers.UserDisplayName: Matthew Carleton
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-15T12:36:51.467Z
Answers.LastActivityDate: 2016-11-15T15:19:44.900Z
Answers.LastEditDate: 2016-11-15T15:19:44.900Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <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>
  
Tags: <categories><pagination>
Title: filtering entries by category
ThreadText: # 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 %}
  <section class="news-page section-wrap news-list">
      <h1>Company News</h1>
      {% if pageInfo.currentPage == 1 %}       
      {% for featuredEntry in craft.entries.section('news').featured('1') %}
          <article class="news-list--item news-list--feature">
              <div class="feature-img">
                  {% for image in featuredEntry.featuredImage %}
                      <img src="{{ image.getUrl('featuredArticleImage') }}" alt="{{ featuredEntry.title }}"/>
                  {% endfor %}
              </div>
              <div class="feature-content">
                  <h1><a href="{{ featuredEntry.url }}">{{ featuredEntry.title }}</a></h1>
                  {{ featuredEntry.body.getPage(1) }}
                  <a class="btn btn-secondary" href="{{ featuredEntry.url }}">Continue reading</a>
              </div>
          </article>
      {% endfor %}
      {% endif %}
      {% for regularEntry in pageEntries %}
          <article class="news-list--item">
              <a href="{{ regularEntry.url }}">
                  <h2>{{ regularEntry.title }}</h2>
                  {{ regularEntry.body|hacksaw(words='25', allow='<p><b>', append='...') }}
                  <span class="cta-border">Continue reading</span>
              </a>
          </article>
      {% endfor %}
  
  </section>
  <nav class="pagination">
      {% if pageInfo.prevUrl %}<a href="{{ pageInfo.prevUrl }}">Previous Page</a>{% endif %}
      {% if pageInfo.nextUrl %}<a href="{{ pageInfo.nextUrl }}">Next Page</a>{% endif %}
  </nav>
  {% endblock %}
  
  {% nav category in craft.categories.group('news') %}
  <li><a href="{{ category.url }}">{{ category.title }}</a></li>
  {% 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`](https://craftcms.com/docs/relations#the-relatedTo-param) 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!  
  
  ---
  
  