1

Any help here would be appreciated… I feel like this is really close.

I have 4 entry types, all of which are shown in a blog index:

  1. Article
  2. Video
  3. Image
  4. Blurb

The most recent entry of the Article type should always be shown as a "featured article" at the top of the page and never repeated again; all the other entry types should appear below this single featured entry in chronological order. The featured article should only appear on the first page of the index, and never appear on subsequent "pages" because this page also uses infinite scroll.

The problem is that the featured article shows up again on all pages past the first. When I remove pagination, but use the same nonFeaturedIds parameter to get my entries, everything works perfectly.

In other words, it seems that the "featured article," is not properly being excluded from Pagination by the entry paramaters I've set up.

Thank you!

{# Define a variable for all the post types #}
  {% set allIds = craft.entries.section('news').ids() %}
{# Define a variable for the featured article: the first occurance of the article entry type #}
  {% set featuredId = craft.entries.type('article').limit('1').ids() %}
{# Define all other entries and entry types as non featured #}
  {% set nonFeaturedIds = allIds|without(featuredId) %}

{# Get the featured entry #}
  {% for entry in craft.entries.id(featuredId) %}
    {% include '/partials/_featuredArticleTemplate' %}
  {% endfor %}

{# set paramaters on an elementCriteriaModel to get all non featured articles and paginate #}
  {% set entries = craft.entries.id(nonFeaturedIds) %}
  {% paginate entries.limit(10) as pageCount, nonfeatured %}

{# Take us to 404 if the page doesn't exist. Otherwise, Infinite scroll keeps repeating the last page of content. #}
  {% if craft.request.getPageNum() > pageCount.totalPages %}
    {% exit 404 %}
  {% endif %}

{# load up the templates for each entry type #}
  {% for entry in nonfeatured %}
    {# all the other articles #}
    {% switch entry.type %} 
      {% case "article" %}
          {% include '/partials/_articleTemplate' %}
      {% case "video" %}
        {% include '/partials/_videoTemplate' %}
      {% case "image" %}
        {% include '/partials/_imageTemplate' %}
      {% case "blurb" %}
        {% include '/partials/_blurbTemplate' %}
      {% default %}
    {% endswitch %}
  {% endfor %}

{# infinite scroll loading area #}
  {% if pageCount.nextUrl %}
    {# pagination styles #}
  {% endif %}
3

You'd have to add a conditional and render the featured entry on the first paginated page only. You can access that info from the pageInfo variable.

Another thing I'd do is to remove the featured entry from the all entries criteria model by prepending the ID with 'not' in the id param (i.e. id('not 72')). You can probably reduce the amount of database queries with that change.

{# Get the featured entry #}
{% set featuredEntry = craft.entries.section('news').type('article').first() %}

{# Get all other entries #}
{% set idParam = 'not ' ~ featuredEntry.id %}
{% set entries = craft.entries.section('news').id(idParam) %}

{% paginate entries.limit(10) as pageInfo, pageEntries %}

{# Render the featured entry for the first page only #}
{% if pageInfo.currentPage == 1 %}
    {% include '/partials/_featuredArticleTemplate' with {
        entry: featuredEntry
    } %}
{% endif %}

{# Render all the other entries #}
{% for entry in pageEntries %}
    {# include partial #}
{% endfor %}
|improve this answer|||||
  • Way more elegant than the way I was doing it, and works flawlessly! Thank you so much! – Doug Hahn Mar 29 '16 at 21:22

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.