Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let's say I have 4 entry types (Article, Video, Photo & Quote). What's the best way to list the latest 5 entries by each entry type?

share|improve this question

craft.entries() fetches entries.

To fetch the entries with a given entry type, you use type. So to get the Article entries, either craft.entries.type('Article') or craft.entries({type: 'Article'})

To fetch the latest 5, you want the entries sorted by postDate, descending (this is the default sort order), and then limit that to 5. Combining with the entry type, you have: craft.entries.type('Article').limit(5) or craft.entries({type: 'Article', limit: 5})

And you have four entry types, so might as well loop over those:

{% for entryType in ['Article', 'Video', 'Photo', Quote] %}
  {% set latestFive = craft.entries({
    type: entryType,
    limit: 5
  }) %}

  {% for oneEntry in latestFive %}
    {# list that entry #}
    {{ oneEntry.title}} is of type {{ entryType }}
  {% endfor %}

{% endfor %}
share|improve this answer
    
So simple <3 Thanks Marion! – Hafa Jul 21 '14 at 19:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.