Forum

Thread tagged as: Problem, Runway

Collection filter using categories

So, I'm new to perch and need some help. I have a list of content, which I want the user to filter using some categories I have setup. This is where I'm at: 1. I have created a category for a listing of items (work type: All, Branding, Property, Web) 2. I have created a collection list of all items. 3. Now I want to output those categories as a form of radio options, so user can select one and see a filtered list of those items. I have created a form of radio's:

<perch:form id="filter" method="get">
  <ul>
    <perch:input id="categoryFilter" type="radio" options="All, Branding, Property, Web" class="radio" wrap="li" />
  </ul>
</perch:form>

and this is what my template looks like:

<div class="c-filters">
      <?php perch_form('work_filters.html'); ?>
    </div>

    <div class="c-listing">
      <?php
        perch_collection('Work items', array(
          'template' => 'work-items-landing.html',
          'page' => '/our-work.php',
          'category' => perch_get('work-type/Branding'),
          'category-match' => 'any',
        ));
      ?>
      <div id="loadMore" class="loadMore">
        <a href="#" class="c-btn c-btn-clear--black">Load More</a>
      </div>

I know the template 'perch_collection' isn't quite right, and im not sure how to link up the radio output to the category 'work-type'. Any help very welcome.

Phil Judge

Phil Judge 0 points

  • 2 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

perch_get() is a wrapper to the global $_GET array, so basically you're asking for the value of the named parameter from the URL query string. work-type/Branding looks like a category path. So if you were hardcoding the category you'd use

'category' => 'work-type/Branding',

and then if you wanted to make it dynamic based on a value from the URL, you'd use

'category' => perch_get('name-of-argument-from-url-to-use'),

Thanks.

So if I want the categories to be dynamic based on my 4 I've created, do i need to create a separate array for each?

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Phil,

The concepts you need to be familiar with:

Assuming when you submit the form the URL is /our-work?type=branding, you can do something like the following:

if(perch_get('type')) {
$category = 'work-type/' . perch_get('type');
} else {
$category = false;
}

perch_collection('Work items', array(
'template' => 'work-items-landing.html',
'category' => $category,
));

Note that Runway Collections are not tied to pages. So you don't need the 'page' => '/our-work.php',

Thanks