Forum

Thread tagged as: Question, Runway

Products from collection with multiple categories

I have a page that is displaying the products for each category using the following code:

<?php
  perch_collection('Products', [
    'category' => 'product-sector/'.perch_get('cat'),
    'template' => 'collections/product_sector.html',
  ]);
?>

On the template 'product_sector.html', I am trying to pull in the current category title:

<h2><perch:categories id="product_sector" label="Product Sector"><perch:category id="catTitle" /></perch:categories></h2>

The issue I have is products can have multiple categories, so on the page I am getting all categories associated with the first item listed in the <h2> e.g. Cat A, Cat C etc

Can someone point me in the right direction to just pull in the current category?

Andy Knight

Andy Knight 1 points

  • 4 years ago

Andy, It seems like I remember this kind of filter has something to do with catDepth, just something to research while waiting for another reply.

Drew McLellan

Drew McLellan 2638 points
Perch Support

By current, do you mean the one you have the slug for as perch_get('cat') ?

Hi Drew,

Yes, exactly that.

Robert Ketter said:

Andy, It seems like I remember this kind of filter has something to do with catDepth, just something to research while waiting for another reply.

Hi Robert, Thanks for the info - I have been looking in to this as well but I haven't got a working solution as yet.

Drew McLellan said:

By current, do you mean the one you have the slug for as perch_get('cat') ?

Hi Drew,

Did you have a solution at all?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, you can use perch_category(perch_get('cat')) to get details of that specific category.

Drew McLellan said:

Yes, you can use perch_category(perch_get('cat')) to get details of that specific category.

Sorry Drew, Accidentally pressed 'mark as solution' instead of quote.

Im not sure how to implement this perch_category(perch_get('cat')) in the code I posted?

Drew McLellan

Drew McLellan 2638 points
Perch Support

If you want to do it in a template instead, you can pass the slug you have into the template and then compare it with a perch:if tag within the perch:categories tags.

Drew McLellan said:

If you want to do it in a template instead, you can pass the slug you have into the template and then compare it with a perch:if tag within the perch:categories tags.

Hi Drew,

Thanks for the info. For others, here is what i did to get this working:

On the page:

<?php PerchSystem::set_var('current_category', perch_get('cat')); ?>

In the template:

<perch:categories id="product_sector" label="Product Sector">
              <perch:if id="catSlug" match="eq" value="{current_category}">
              <h2><perch:category id="catTitle" /></h2>
              </perch:if>
              </perch:categories>