Forum

Thread tagged as: Question

Display all categories with list/detail items

I'm trying to create a category listing with Case Study list/detail items like this:

Category 1
- case study 1
- case study 2 
... etc

Category 2
- case study 3
- case study 4
... etc

so that I loop through all available categories that have list/detail items

So far, I can display a single category and its associated case studies as follows:

perch_categories(array(
                'set'=>'Case Studies',
                'filter'=> 'catTitle',
                'match' => 'eq',
                'value' => 'categoryname'
            )); 

            perch_content_custom('Case Studies', array(
                'template' => 'casestudy_category_listing.html',
                'category' => 'case-studies/categoryname'

        ));

but I'm struggling with how to display all categories like this. I could repeat this multiple times but I'd like to loop through this (I think). Can you point me towards the best way of doing this. Thank you.

Clive Walker

Clive Walker 22 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

  1. Grab the perch_categories() call into an array using skip-template

  2. Loop through that array calling perch_content_custom() using the value from the array for the category to filter by

I'm OK with step 1 but step 2 is a struggle. I can see the array data on the page using print_r but that's as far as I have got.

Drew McLellan

Drew McLellan 2638 points
Perch Support

What does the print_r display?

As follows:

Array ( [0] => Array ( [desc] => [catID] => 3 [setID] => 2 [catParentID] => 0 [catTitle] => Stockade [catSlug] => stockade [catPath] => case-studies/stockade/ [catDisplayPath] => Stockade [catOrder] => 2 [catTreePosition] => 002-002 [catDynamicFields] => {"desc":{"raw":"","processed":""}} [perch_desc] => [catDepth] => 1 ) [1] => Array ( [desc] => [catID] => 8 [setID] => 2 [catParentID] => 3 [catTitle] => Sub-category [catSlug] => sub-category [catPath] => case-studies/stockade/sub-category/ [catDisplayPath] => Stockade › Sub-category [catOrder] => 1 [catTreePosition] => 002-002-001 [catDynamicFields] => {"desc":{"raw":"","processed":""}} [perch_desc] => [catDepth] => 2 ) [2] => Array ( [desc] => [catID] => 4 [setID] => 2 [catParentID] => 0 [catTitle] => Facilities Management [catSlug] => facilities-management [catPath] => case-studies/facilities-management/ [catDisplayPath] => Facilities Management [catOrder] => 3 [catTreePosition] => 002-003 [catDynamicFields] => {"desc":{"raw":"","processed":""}} [perch_desc] => [catDepth] => 1 ) [3] => Array ( [desc] => [catID] => 5 [setID] => 2 [catParentID] => 0 [catTitle] => Proof of Delivery [catSlug] => proof-of-delivery [catPath] => case-studies/proof-of-delivery/ [catDisplayPath] => Proof of Delivery [catOrder] => 4 [catTreePosition] => 002-004 [catDynamicFields] => {"desc":{"raw":"","processed":""}} [perch_desc] => [catDepth] => 1 ) [4] => Array ( [desc] => [catID] => 6 [setID] => 2 [catParentID] => 0 [catTitle] => Production Monitoring [catSlug] => production-monitoring [catPath] => case-studies/production-monitoring/ [catDisplayPath] => Production Monitoring [catOrder] => 5 [catTreePosition] => 002-005 [catDynamicFields] => {"desc":{"raw":"","processed":""}} [perch_desc] => [catDepth] => 1 ) [5] => Array ( [desc] => [catID] => 7 [setID] => 2 [catParentID] => 0 [catTitle] => Automated Print and Apply [catSlug] => automated-print-and-apply [catPath] => case-studies/automated-print-and-apply/ [catDisplayPath] => Automated Print and Apply [catOrder] => 6 [catTreePosition] => 002-006 [catDynamicFields] => {"desc":{"raw":"","processed":""}} [perch_desc] => [catDepth] => 1 ) ) 
Drew McLellan

Drew McLellan 2638 points
Perch Support

foreach($categories as $category) {
   perch_content_custom('Case Studies', array(
       'template' => 'casestudy_category_listing.html',
       'category' => $category['catPath']

   ));
}

OK! Thank you.

I am wanting to do pretty much the same thing, but as follows:

<h2>Category A Name</h2>
<p>Category A Desc</p>
<table>
 <tr>
  <td>Publication 1 title</td><td>Download link</td>
 </tr>
<tr>
  <td>Publication 2 title</td><td>Download link</td>
 </tr>
</table>

<h2>Category B Name</h2>
<p>Category B Desc</p>
<table>
 <tr>
  <td>Publication 3 title</td><td>Download link</td>
 </tr>
<tr>
  <td>Publication 4 title</td><td>Download link</td>
 </tr>
</table>

What would be the full code needed to produce this all on one page?