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 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>
    {% 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>
    {% endfor %}

<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 %}
{% endblock %}

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

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.

| improve this answer | |
  • 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 Nov 15 '16 at 15:00
  • How do you build the "category navigation" currently? Sounds like your hrefs are simply off a bit. – Mats Mikkel Rummelhoff Nov 15 '16 at 15:02
  • ya you are probably right. See my edit at the bottom. – Matthew Carleton Nov 15 '16 at 15:05
  • 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 Nov 15 '16 at 15:35
  • ah, nevermind adding .relatedTo(category) to the featuredEntry did the trick. Thanks! – Matthew Carleton Nov 15 '16 at 15:42

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.