1

I am trying to build an Index page and assign a specific card template to the 3rd entry and only the 3rd entry.

{% set items = craft.entries.section('posts').all() %}

{% for cardC in items.offset(2).limit(1) %}

    <div class="col-md-12">
        <div class="card card-raised card-background" style="background-image: url('{{ cardC.postImage.one().getUrl() }}')">
            <div class="card-body">
                <h6 class="card-category text-info">Marketing</h6>
                <h3 class="card-title">{{ cardC.title }}</h3>
                <p class="card-description">
                   {{ cardC.introParagraph }}
                </p>
                <a href="" class="btn btn-warning btn-round">
                    <i class="material-icons">subject</i> Read Case Study
                </a>
                <a href="" class="btn btn-white btn-just-icon btn-link" title="" rel="tooltip" data-original-title="Save to Pocket">
                    <i class="fa fa-get-pocket"></i>
                </a>
            </div>
        </div>
    </div>

{% endfor %}

I also tried {% for cardC in items.nth(3 )%}.

I get the following **error message**:

Impossible to invoke a method ("offset") on an array.

I created the set because I need to apply templates for the other entries, etc.

2

As soon as you apply .all(), the variable you're working with becomes an array. Which means that the .offset(2) needs to be applied before you convert it into an array.


UPDATE

As @andrew.welch points out in the comments, it's as easy as grabbing the third item in your array...

items[2]

Ultimately, it looks like you want something like this...

{% set cardC = items[2] %}

{% if cardC %}

    {# ... the rest of your code #}

{% endif %}

ORIGINAL ANSWER

Once it's an array, you'll need to use the Twig syntax instead...

items[2:1]

Which translates to: "get third item (0-based index), limit of one".

| improve this answer | |
  • should I keep the .all()? – Hellyar Jun 2 '18 at 16:25
  • You mentioned that you set items in order to render multiple entries. If so, then yes... That said, don't apply the offset & limit before the all if you intend to use items for multiple entries. – Lindsey D Jun 2 '18 at 16:27
  • Do we need slice here? Seems like it could just be items[2] no? – andrew.welch Jun 2 '18 at 17:32
  • Ahh, good call @andrew.welch! Even I was overcomplicating it. – Lindsey D Jun 2 '18 at 21:20
  • I've updated my answer accordingly. – Lindsey D Jun 2 '18 at 21:21

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.