2

I have a sort of odd need and I'm not exactly sure how to solve it so I'm coming here for help. I have a channel entries loop that displays all the latest posts for our site with pagination on our home page. We want to now add a new channel, but for it's entries we only want the most recent entry from that channel to show up in the main all channel channel entries loop. Is something like this possible? If so, how would you do it?

5
  • 1
    Does the single entry from the other channel need to appear somewhere between entries from the other main loop? Or just at the top (or bottom)? If it's the latter, I'd just use two separate channel entries tags.
    – Tyssen
    Commented Jan 4, 2013 at 2:51
  • Tyssen, the idea was that it would ideally fit appropriately in-line within the entire channel entries loop based on the post date. The second best option would be it appearing at a set point, say the 3rd item down, but that is less than ideal. Commented Jan 4, 2013 at 5:48
  • 1
    Thanks Nate, I'll do that now. I didn't really understand that was part of the process. Guess I'm too used to a traditional style forum system. Commented Jan 4, 2013 at 5:52
  • If I understand correctly, you are trying to mix 2 channels entries (latest ones) in one {channel:entries} loop? Does the channels shared the same field group?
    – pvledoux
    Commented Jan 4, 2013 at 12:08
  • @pvledoux: Sort of. Really I just want to somehow control the "limit" parameter on a channel by channel basis with-in the channel entries loop. So I am using the channel entries loop to return all results from channel A and channel B. Inside the loop I want to display all the results from channel A, but just the first result it finds from channel B. This could easily be done with 2 separate channel entries loops, but the reason that won't work is because I also want the results together in order by date. Hopefully this makes sense. Commented Jan 4, 2013 at 18:17

2 Answers 2

1

You could use a combination of Stash, multiple exp:channel:entries loops and a snippet (optionally) to build your solution.

Snippet: sn_set_fields

{exp:stash:append_list name="entries"}
  {stash:title}{title}{/stash:title}
  {stash:channel}{channel}{/stash:channel}
  {stash:id}{entry_id}{/stash:id}
  {stash:date}{entry_date}{/stash:date}
{/exp:stash:append_list}

Template:

{exp:channel:entries channel="channel_1" limit="1"}
  {sn_set_fields}
{/exp:channel:entries}

{exp:channel:entries channel="channel_2" limit="1"}
  {sn_set_fields}
{/exp:channel:entries}

<ul>
{exp:stash:get_list name="entries" orderby="date" sort="desc" process="end"}
  <li>{date format="%d/%m/%Y"} from {channel}: {title}</li>
{/exp:stash:get_list}
</ul>

As people have suggested, use the multiple channel entries loops so you can take advantage of the limit of 1 per channel. But using Stash, you're able to combine them and control the output of them as if they were one.

Smarter people than me will be able to comment on performance issues with this.

1

With just the channels entry tag, this can't be done. As you can an imagine, you can tell the tag to pull from two or more channels, but you can't tell it you only want a single entry from channel 'b'. So…

{exp:channel:entries channel="news|events" limit="5" orderby="entry_date" sort="desc"}
  {fields}
{/exp:channel:entries}

Would get you the five most recent items, but no guaranteed ratio of news vs events. You could use a custom query to pull the items from the database as you like, or as @JasonVarga mentions Stash could be used to pull the queries individually, then group them together.

Your Answer

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

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