I'm hoping someone can suggest an elegant solution to this. I sometimes need to have the result that a channel:entries loop inside a channel:categories loop can provide. A simple example would be:

<ul> {exp:channel:categories channel="main" style="linear"} <li>{category_name} <ul> {exp:channel:entries channel="main" category="{category_id}"} <li>{title}</li> {/exp:channel:entries} </ul> </li> {/exp:channel:categories} </ul>

This sort of approach allows for categories to be added/removed on the fly without having to alter the template, but I am aware that such nesting can be bad for performance. What is the better way to do this?

One user has suggested using the category_archive tag but since that tag can only grab title or url_title content from entries it would not permit something like this:

<table> {exp:channel:categories channel="courses" style="linear"} <tr><th colspan="5">{category_name}</th></tr> <tr> <th>Course Title</th> <th>Course ID</th> <th># Days</th> <th>Description</th> <th>Price</th> </tr> {exp:channel:entries channel="courses" category="{category_id}"} <tr> <td>{title}</td> <td>{course_code}</td> <td>{course_duration}</td> <td>{course_blurb}</td> <td>{price}</td> </tr> {/exp:channel:entries} {/exp:channel:categories} </table>

Please note that in the above example {course_code}, {course_duration}, {course_blurb} and {price} are all existing entry fields.

I'm pretty sure that you want to be using the Category Archives tag:

https://docs.expressionengine.com/latest/channel/category_archive.html

This should allow you to achieve your desired result with less of a performance hit. I think the tag was made to do exactly what you are looking to do.

  • I have always passed over using the channel:category_archives tag due to it not offering enough flexibility. It appears the only content it can grab from entries is from the title or title_url fields. – thisyONE Dec 5 at 19:57
  • Well, then the only way to be more efficient running that dynamically would be to write a query with the exp:query tag or write a plugin/add-on. And if you are going to do it the way you're trying to avoid in your question, and you are grabbing so many categories and entries that you have to worry about overhead, you should probably rethink your design. The next option is to look into caching. – jrothafer Dec 5 at 21:08
  • 1
    Thanks for the feedback. Truth is, I have never actually observed poor performance from using these nested loops. I just recently found some posts that indicate that nested loops are problematic so I wanted to find out if there are any best practice solutions. I am surprised EE doesn't provide a dedicated means to achieve this - it is something I have a frequent need for (cycling through multiple categories to grab content that can then be displayed in category-related sections). – thisyONE Dec 6 at 16:10
  • Pretty much, your method is an acceptable implementation as long as you don't have a very great number of categories. Some context would've helped me answer your question, like range of number of categories and range of number of entries per category, I might have some other thoughts, but I think you're all set. Rock on. – jrothafer Dec 7 at 6:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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