I have a structure that I am using for a schedule, it has 1 entry per day (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday).

What I want to do is only show the 3 days from the current day.

i.e if today is Thursday, it will show the Thursday, Friday & Saturday entries.

I have used the following to control the offset which works great.

{% for entry in craft.entries.section('schedule').offset(now|date('w')).limit(3).find() %}

My problem is, when I get to Friday or Saturday it wont show Sunday because that is the first entry of the structure.

Is there anyway I can have it loop around to start the list of entries again?


You can't do this with the criteria parameters offset and limit. I would suggest to first get all the IDs of your section and then use Twig's handy cycle function to get those entry IDs you're interested in.

That function starts to read an array from the beginning again, when it reaches it end, so that's exacly what you need here. So when you want to get the 8th element of an array of length 7, it returns the first element (monday)!

The first element / ID of your array is in position 0, so I sustracted 1 from the week day number before passing it to the cycle function.

{% set allIds = craft.entries.section('schedule').ids() %}
{% set nextIds = [] %}

{% set start = now|date('w') - 1 %}
{% set end = start + 2 %}

{% for i in start..end %}
    {% set id = cycle(allIds, i) %}
    {% set nextIds = nextIds|merge([id]) %}
{% endfor %}

{% set entries = craft.entries.id(nextIds).fixedOrder(true) %}

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.