Forum

Thread tagged as: Question, Runway

How to filter by category with Perch Runway?

I've setup a collection called Projects and to this I've added a category set called Project-set

I've added perch:categories id="project-set" to my collection template. When I click the link, I've simplified the template. I get a message Sorry, that page could not be found.

 <perch:categories id="project-set" label="Project categories" set="project-set" required="true">
  <a href="/category/<perch:category id="catPath" />"><perch:category id="catTitle" /></a></li>
  </perch:categories>

I've created a /category page and I have the following PHP on it:

    if (perch_get('cat')) {
     perch_category(perch_get('cat'),array(
    'template'=>'category_single.html'
     ));

    } else {
    perch_categories();
    }

When I visit /category page directly I see a list of all the categories using the default template, as expected.

I have a feeling I have to do something with routing and tokens, can you tell me what I'm doing wrong please?

Stephen Meehan

Stephen Meehan 4 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Is this a typo?

 if (perch_get('get'))

Drew McLellan said:

Is this a typo?

if (perch_get('get'))

Whoops. Yes. Changed it.

Is the reason it's not working something to do with routing? If so, what would I write to make it work?

Hi

Still stuck on this.

Using a Perch Collection with Categories, how do I filter content based on a category?

On my home page I have a collection called Projects

    perch_collection('Projects', [
    'sort' => 'date',
    'sort-order' => 'DESC',
    'count' => 3,

    ]
    );

The Projects collection uses a template that includes perch:categories. This is the bit of the template that outputs a link to my category page:

        //  Category link
       <a href="/category/<perch:category id="catPath" />"><perch:category id="catTitle" /></a>

On my category page I have this PHP

     if (perch_get('cat')) {
     perch_category(perch_get('cat'),array(
    'template'=>'category_single.html'
     ));

    } else {
    perch_categories();
    }

When I click on the category link, the if statement is skipped and the else part is displayed, using the default category template. I'd like to be able to display the category_single.html template when the category link is clicked.

I can't see what I'm missing? Hope you can help.

Hi

I'm still trying to get this to work. I've watched the Portfolio with Categories video, as it has a bit in it about filtering categories, but using the code above (based on the video) I can't get it to work?

What am I doing wrong? I've had something similar to this working with Perch but can't seem to get it working with Perch Runway, I'm sure I'm missing something obvious - I just can't see it.

Drew / Rachael

Any chance you could take a look at this?

How do I use perch_get('cat') with Perch Runway? I've used the PHP below on a Perch website and it works:

        if (perch_get('cat')) {
        perch_category(perch_get('cat'));
                } else {
            perch_categories();
                   }

But the same doesn't seem to work with Perch Runway. I have a feeling it might be related to URL patterns / Routing - but I don't know. I'd really appreciate some guidance on this.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes - what's the value of cat at that point? Is it a full category path? Or just a category slug?

It'd be a full category path. You'd get to by clicking:

         <a href="/category/<perch:category id="catPath" />"><perch:category id="catTitle" /></a>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Have you tested what value you're getting back into the page?

Is this what you mean? This is the message when the URL pattern is ** /category**

  Debug Message
  SELECT p.pagePath, pr.routePattern, pr.routeRegExp, p.pageTemplate FROM perch2_pages p LEFT JOIN       perch2_page_routes pr ON p.pageID=pr.pageID ORDER BY pr.routeOrder ASC
  Using master page: /templates/pages/category.php
  Page arguments:
  SELECT * FROM perch2_pages WHERE pagePath='/category' LIMIT 1
  SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/category' OR regionPage='*'       ORDER BY regionPage DESC
  SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageDepth >=0 AND pageDepth<=1       ORDER BY pageTreePosition ASC
  SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/category' LIMIT 1
  SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-005', '000')
  Using template: /templates/navigation/item.html
  SELECT setID FROM perch2_category_sets WHERE setSlug='' LIMIT 1
  SELECT main.* FROM perch2_categories main WHERE 1=1 ORDER BY catTreePosition ASC
  Using template: /templates/categories/category.html
  SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0
  Time: 0.0436
  Memory: 3.4963

This is what I see when I set the URL pattern to /category/project-set/[slug:cat]

  Debug Message
  SELECT p.pagePath, pr.routePattern, pr.routeRegExp, p.pageTemplate FROM perch2_pages p LEFT JOIN       perch2_page_routes pr ON p.pageID=pr.pageID ORDER BY pr.routeOrder ASC
  Matched route: category/project-set/[slug:cat]
  Using master page: /templates/pages/category.php
  Page arguments:
  Array
  (
[0] => /category/project-set/construction
[cat] => construction
[1] => construction
  )
  SELECT * FROM perch2_pages WHERE pagePath='/category' LIMIT 1
  SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/category' OR regionPage='*'       ORDER BY regionPage DESC
  SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageDepth >=0 AND pageDepth<=1       ORDER BY pageTreePosition ASC
  SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/category' LIMIT 1
  SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-005', '000')
  Using template: /templates/navigation/item.html
  SELECT setID FROM perch2_category_sets WHERE setSlug='' LIMIT 1
  SELECT main.* FROM perch2_categories main WHERE 1=1 AND (catPath='construction/') ORDER BY catTreePosition             ASC
  Using template: /templates/categories/category.html
  SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0
  Time: 0.0377
  Memory: 3.4839
Drew McLellan

Drew McLellan 2638 points
Perch Support

You've got no page arguments coming in. What route have you set up for this?

I updated the previous post. The URL pattern is /category/project-set/[slug:cat]

Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you give an example of the URL?

https://local-demo-runway/category/project-set/construction/

It's a Perch Runway site, running locally via MAMP Pro

When I click on the link

   <a href="/category/<perch:category id="catPath" />"><perch:category id="catTitle" /></a>

The PHP doesn't work, neither the if or the else statements outputs anything, but the rest of the page displays.

 //  This is on /category  
  if (perch_get('cat')) {
  perch_category(perch_get('cat'));

    } else {
 perch_categories();

 }

Any further thoughts on this? I've pulled everything into one post, hopefully it will make it clearer.

Would you set it up any differently?

home has a perch_collection that contains a template with this link:

     // This links to the category page
    <a href="/category/<perch:category id="catPath" />"><perch:category id="catTitle" /></a>

Clicking this link (on the home page) outputs this URL:

    https://local-demo-runway/category/project-set/construction/

This is the URL pattern for the category page:

  category/project-set/[slug:cat]

On the category page I have this PHP:

        if (perch_get('cat')) {
        perch_category(perch_get('cat'));

        } else {
       perch_categories();
        }

The page loads, but the PHP doesn't trigger.

Here's the debug message:

     Debug Message
     SELECT p.pagePath, pr.routePattern, pr.routeRegExp, p.pageTemplate FROM perch2_pages p LEFT JOIN          perch2_page_routes pr ON p.pageID=pr.pageID ORDER BY pr.routeOrder ASC
     Matched route: category/project-set/[slug:cat]
     Using master page: /templates/pages/category.php
     Page arguments:
     Array
     (
         [0] => /category/project-set/construction
         [cat] => construction
         [1] => construction
     )
     SELECT * FROM perch2_pages WHERE pagePath='/category' LIMIT 1
     SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/category' OR          regionPage='*' ORDER BY regionPage DESC
     SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageDepth >=0 AND          pageDepth<=1 ORDER BY pageTreePosition ASC
     SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/category' LIMIT 1
     SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-005', '000')
     Using template: /templates/navigation/item.html
     SELECT setID FROM perch2_category_sets WHERE setSlug='' LIMIT 1
     SELECT main.* FROM perch2_categories main WHERE 1=1 AND (catPath='construction/') ORDER BY          catTreePosition ASC
     Using template: /templates/categories/category.html
     SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0
     Time: 0.0394
     Memory: 3.4841

Anyone else having this problem? Trying to filter categories from within a perch_collection

Drew McLellan

Drew McLellan 2638 points
Perch Support

You need to use a full category path, not just the category slug as you currently have.

Ah, ok

How do I add the full category path to this:

   if (perch_get('cat')) {
    perch_category(perch_get('cat'));

    } else {
   perch_categories();
    } 

Is there a docs page I can refer to?

Done!

       if (perch_get('cat')) {
  perch_category('/project-set/'.perch_get('cat'),array(
    'template'=>'category_single.html',
     ));
    } else {
 perch_categories();

}

I added the full category path to perch_category

  perch_category('/project-set/'.perch_get('cat'),array(

Thanks for pointing me in the right direction!