0

I'm having a bit of trouble with this, and I can't even describe it very well. Anyways, what I want to do, is list through all the entries I have in a certain channel, just like usual. Except I want to sort it by categories. So on the page, I want category 1 title, then all the entries assigned to category 1, then category 2 title, and all the entries assigned to category 2, etc. Like this:

**CATEGORY 1 TITLE**
 _______
|       | {title}
| {img} | {description}
|_______|
 _______
|       | {title}
| {img} | {description}
|_______|

**CATEGORY 2 TITLE**
 _______
|       | {title}
| {img} | {description}
|_______|

**CATEGORY 3 TITLE**
 _______
|       | {title}
| {img} | {description}
|_______|
 _______
|       | {title}
| {img} | {description}
|_______|

Hopefully that makes sense. This is what I've tried,

{exp:channel:entries channel="mychannel"}

{exp:channel:categories channel="mychannel" style="linear"}
    <h2>{category_name}</h2>

    <img src="{img}" alt="{alt}">
    <h3>{title}</h3>
    <p>{description}</p>

{/exp:channel:categories}

{/exp:channel:entries}

But that doesn't work at all, because for each entry it shows all the category titles, and then under each title displays that entry's info once.

I really hope that was clear enough. Any help would be greatly appreciated. Thank you

1

Your solution will not perform or scale well. Instead use the native category_archive tag: https://ellislab.com/expressionengine/user-guide/add-ons/channel/category_archive.html (though even that suffers from poor performance).

| improve this answer | |
  • I left it my way for that past project, but I'm doing a similar thing now and I'm going to do it this way. Could you please explain what you mean that it won't "perform or scale well"? – eshellborn Dec 11 '14 at 22:45
  • The channel:entries loop is very heavy and slow and should be used sparingly - yet what you're doing is putting it inside another loop (the channel:categories tag). This means that every category that gets added by your editors will cause another channel entries loop to run (actually two in your example!). As your content grows your site will render more and more slowly as it churns through all the processing. Take a look at this: gotolow.com/blog/nesting-tags-and-performance-in-ee – James Smith Dec 11 '14 at 23:10
0

So I think I got it. I mostly just needed to swap the nesting of the tags:

{exp:channel:categories channel="mychannel" style="linear"}

{exp:channel:entries channel="mychannel" category="{category_id}" limit="1"}
    <h2>{category_name}</h2>
{/exp:channel:entries}

{exp:channel:entries channel="mychannel" category="{category_id}"}
    <img src="{img}" alt="{alt}">
<h3>{title}</h3>
<p>{description}</p>
{/exp:channel:entries}

{/exp:channel:categories}

I also had to use the channel:entires tag twice to make sure the heading only appears if there are actually entries.

| improve this answer | |
  • If you add show_empty="no" as a parameter on the categories loop, you would then be able to eliminate the extra entries loop because then the heading would still only show if there are entries assigned to that category. – Jean St-Amand Dec 3 '14 at 14:22

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.