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"}

1 Answer 1

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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