2

I have the following code for my news page. The links show up but I'm seeing all the entries on every page. I'm not sure if this is related to my featured entry at the top.

{% extends "_layout" %}
{% set title = "News" %}
{% paginate craft.entries.section('news').limit(5) as pageInfo, pageEntries %}
{% block content %}
<section class="news-page section-wrap news-list">
<h1>Company News</h1>
{% 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 %}
{% for regularEntries in craft.entries.section('news').featured('not 1') %}
    <article class="news-list--item">
            <a href="{{ regularEntries.url }}">
            <h2>{{ regularEntries.title }}</h2>
            {{ regularEntries.body|hacksaw(words='25', allow='<p><b>') }}
            <span class="cta-border">Continue reading</span>
            </a>
    </article>
{% endfor %}

</section>  
<nav>
{% if pageInfo.prevUrl %}<a href="{{ pageInfo.prevUrl }}">Previous Page</a>{% endif %}
{% if pageInfo.nextUrl %}<a href="{{ pageInfo.nextUrl }}">Next Page</a>{% endif %}
    </nav>
{% endblock %}
2

When you use {% paginate %}, the second variable that's returned, pageEntries, is the paginated entries. When you do craft.entries.section('news').featured('not 1') later in the template, you're doing a new query that's not paginated.

Assuming what you want is to return all featured entries at the top, then a paginated list of news below, try something like this:

{% extends "_layout" %}
{% set title = "News" %}

{% paginate craft.entries.section('news').featured('not 1').limit(5) as pageInfo, pageEntries %}

{% block content %}
    <section class="news-page section-wrap news-list">
        <h1>Company News</h1>
        {% 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 %}

        {% 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>') }}
                    <span class="cta-border">Continue reading</span>
                </a>
            </article>
        {% endfor %}

    </section>
    <nav>
        {% if pageInfo.prevUrl %}<a href="{{ pageInfo.prevUrl }}">Previous Page</a>{% endif %}
        {% if pageInfo.nextUrl %}<a href="{{ pageInfo.nextUrl }}">Next Page</a>{% endif %}
    </nav>
{% endblock %}

If additionally, you only want to show the featured entry on the first page, you could wrap the first for loop in:

{% if pageInfo.currentPage == 1 %}
    ...
{% endif %}

More details on pagination in the docs.

| improve this answer | |
  • ah, all that works well except removing the featured for any other page but the first. That if statement doesn't seem to be work. Thoughts? – Matthew Carleton Nov 14 '16 at 20:05
  • @MatthewCarleton I just updated the code for the conditional that checks if the first page is being displayed. – carlcs Nov 14 '16 at 20:42
  • Yes! that does it. – Matthew Carleton Nov 14 '16 at 21:03
  • Ah, I was a bit too quick reading the docs there. Thanks, Carl. :) – André Elvan Nov 14 '16 at 21:58

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.