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

1 Answers1

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.

carlcs
  • 36,220
  • 5
  • 62
  • 139
André Elvan
  • 7,288
  • 22
  • 34