2

I have multiple entries in 'blog' that have the 'featurePost' checkbox field checked. I want to display ONLY the latest, but this code is displaying multiple fields.

{% paginate craft.entries({ section: 'blog', order: 'featurePost desc, postDate desc', limit: 5 }) as entriesOnPage %}

{% for entry in entriesOnPage %}
    {% if craft.entries.section('blog').featurePost.limit(1) | length %}
        <article class="flex-container-small {{ entry.featurePost | length ? 'pinned' }}">
    {% else %}
        <article class="flex-container-small">
    {% endif %}
{% endfor %}

{% include "partials/blog/_blog-pagination" %}

{% endpaginate %}
3
  • Is this inside a loop? Can you update with the loop code too?
    – Rick
    Oct 5 '15 at 21:32
  • Yes, I just updated to include the loop.
    – seth
    Oct 6 '15 at 0:16
  • To clarify, you want to have a paginated entry loop where you first show the latest featured post, followed by all of the rest of the entries, ordered by Post Date (desc), right? What should happen on subsequent pages? Just show the next 5 entries? Or should each page start with the same latest Featured Post entry? Oct 6 '15 at 0:54
1

Edit: First update your checkboxes field to a lightswitch field. The checkboxes field type stores its data to the DB as a serialized array, so you can't use the field for the order parameter or a custom field parameter.

--

To pass the same amount of entries to each of your paginated pages I would suggest the following approach.

Build an array of entry IDs in your desired sort order. You have to query twice using the ids() method and merge the returned IDs. First query is for the featured entries, where you add your featurePost field parameter, second one for all your section's entries. Before merging take care to not include the featured entries IDs twice by using Twigs without filter.

You can now query for the entry models by passing that IDs array to the id parameter and adding fixedOrder(true).

{% set featureEntryIds = craft.entries({
    section: 'blog',
    featurePost: 1,
    limit: 1
}).ids() %}

{% set allEntryIds = craft.entries({
    section: 'blog'
}).ids() %}

{% set sortedEntryIds = featureEntryIds|merge(
    allEntryIds|without(featureEntryIds)
) %}

{% paginate craft.entries({
    id: sortedEntryIds,
    fixedOrder: true,
    limit: 5
}) as entriesOnPage %}

    {% for entry in entriesOnPage %}
        {% set articleClass = entry.id in featureEntryIds ? 'pinned' %}
        <article class="{{ articleClass }}">{{ entry.title }}</article>
    {% endfor %}

{% endpaginate %}
0
1

This should show all entries, paginated, with featured posts at the start, but not pinned to the top of each page.

{% paginate craft.entries({ section: 'blog', order: 'featurePost desc, postDate desc', limit: 5 }) as entriesOnPage %}

    {% for entry in entriesOnPage %}
        <article class="flex-container-small{% if entry.featurePost %} pinned{% endif %}">
    {% endfor %}

    {% include "partials/blog/_blog-pagination" %}

{% endpaginate %}

Ordering by featurePost is good, but you can perform the check a lot more simply.

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.