Forum
Runway: Looping Collections by Category?
What would be the right way to loop through the collections placing them under a new category? I'm trying to output multiple collection entries under separate categories on the same page.
Currently the following outputs the categories but the same category is repeated with just one collection entry each time.
archive.php
<?php
perch_collection('Knowledge Articles', [
'category' => 'knowledge-base/'.perch_get('cat'),
'template' => 'cpi/listings/knowledge_archive.html',
]);
?>
knowledge_archive.html
<div class="card card-5">
<h3>
<perch:categories id="categories" set="departments" label="Categories" display-as="dataselect">
<a href="/knowledge-base/category/<perch:category id="catSlug" type="slug" />" class="p-category">
<perch:category id="catTitle" type="text" />
</a>
</perch:categories>
</h3>
<div class="card__menu">
<ul class="side-nav">
<li>
<a href="">
<span class="cat__icon"><i class="ss-icon">📓</i></span>
<perch:content id="desc" type="text" label="Title" order="1" required="true" title="true" />
</a>
</li>
<li>
<perch:categories id="categories" set="departments" label="Categories" display-as="dataselect">
<a href="/knowledge-base/category/<perch:category id="catSlug" type="slug" />" class="p-category">
<span class="cat__icon"><i class="ss-icon">➡</i></span>
View All Articles
</a>
</perch:categories>
</li>
</ul>
</div>
</div>
If you order by category, you can then use
<perch:if different="">
to only output then heading when it changes.So are you saying place
'sort' =>'categories',
in the function to order by category? Not sure what you mean by "Order by".Yes, that's what I mean. Order by as in sort or arrange in that order.
Ok, I added the sort by categories but it shows none of the collection content. I have included my degbug code below "just in case".
Hmm. Try this:
Thanks Drew - That seems to work - though I'm not sure why.
Hi Drew
I have been using
<perch:if different="">
to output the headings above each category list as you advised. How would I wrap/encase each category list in a<div>
detecting where the category changes?So in basic form i'm wanting it to look like the following...
Currently it looks like the following...
If it's a different category, and it's not the first item, output a
</div>
.Thanks Drew!
How would I count 5 items in a category
How would I show only 5 items per category? - I have tried using <perch:every> to do this but noticed that it skips an item out of the list.
<perch:every count="5">
will output every 5 items, not the first 5.You should be able to use nth-child for this. Try
-n+5