1

In Expression Engine, how would I go about retrieving the latest 3 entries from a channel and then loop through the result set in reverse? I tried something like this to no avail :

{exp:channel:entries channel="news_events" limit="3" orderby="entry_id|date" sort="desc|asc"}

0

I don't believe there's a way to do what you are asking with a single channel:entries.

One way or another you are going to have to make two passes through the data. Here are some ways:

  • exp:query + channel:entries
  • php + channel:entries
  • channel:entries + embed + channel:entries
  • channel:entries + re-sorting in the results on the client side with javascript

Here's the a working example of the channel:entries + embed + channel:entries method:

template 1: (this is the template you call in your browser)

{embed="my_template_group/my_embedded_template"
my_entry_ids='
{exp:channel:entries channel="news_events" limit="3" disable="categories|custom_fields|pagination|member_data" backspace="1"}{entry_id}|{/exp:channel:entries}
'}

Here we are passing three piped delimited entry_ids as an embed variable to another template called my_template_group/my_embedded_template. Note well the single vs double quotes here.

template 2 (referenced above as my_template_group/my_embedded_template)

<ul>
{exp:channel:entries channel="news_events" disable="categories|custom_fields|pagination|member_data" fixed_order="{embed:my_entry_ids}" sort="desc"}<li>{entry_id}:{title}</li>
{/exp:channel:entries}
</ul>

Here we have another channel:entries call. The key is that we are injecting our entry_ids from the parent template in to the fixed_order parameter here and then flipping the sort to desc.

| 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.