3

Hey everyone I am trying to output entries on the homepage. My goal is that the most recent entry is styled differently. I achieved this by calling the loop too times: {% for entry in craft.entries.section('blogPosts').limit(1) %} than {% for entry in craft.entries.section('blogPosts').all() %} and it works perfectly. My only issue is that I want to exclude the most recent post from the second loop, so it only appear in the first loop.

Is there a way in craft for exactly the most recent entry or I should do this with ID?

Thank you for your help!

3

Best practice is to only query for the data once, then perform any necessary filtering on the result, rather than making multiple expensive calls to craft.entries...

So in your case you could take the first entry using [0], and then use Craft's without filter to filter out the first entry from the list:

{% set posts = craft.entries.section('blogPosts').all() %}
{% set firstPost = posts[0] %}
{% set otherPosts = posts|without(firstPost) %}
| improve this answer | |
1

The easiest way to accomplish this, is by adding an offset of 1.

Result:

{% for entry in craft.entries.section('blogPosts').offset(1).all() %}

You can find it in the documentation here.


Also, instead of getting all the entries and limiting it to 1, you can use .first()

craft.entries.section('blogPosts').first()
| improve this answer | |

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.