Forum

Thread tagged as: Question, Runway

Categories and Perch Runway

Hi Guys,

I have never really used categories much and so I am a little stuck/lost. Wondered if you can perhaps give me some direction.

I have created a list and details page and I want to be able to filter the stories based on the categories. So if you click one of the categories in then it would show you a list of all stories for that category.

I have setup my cats in the backend and I have my list and details page working. But not I am trying to filter I am stumped. I am not sure what I need to setup in order to achieve this? I assume I would do the filtering with some if statements on the same stories.php template where my list/details are shown, but as this is runway I am also unsure about what routes to add etc?

Thanks in advance
Terry

Terry Upton

Terry Upton 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

What do you want the URLs to be?

For example;

stories/age-group/0-5/
stories/age-group/6-10/

stories/diagnosis/acute-lymphoblastic-leukaemia/
stories/diagnosis/acute-myeloid-leukaemia/

Drew McLellan

Drew McLellan 2638 points
Perch Support

Are age-group and diagnosis your category sets?

Yes they are

Drew McLellan

Drew McLellan 2638 points
Perch Support

What does your detail page URL look like?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, so I think you need routes like

stories/[slug:set]/[slug:cat]
stories/[slug:story]

Then logic like:

if (perch_get('story') {
   // ... detail mode ...
} else {
   // ... list mode ...

    if (perch_get('cat')) {
        $catPath = perch_get('set') .'/'. perch_get('cat');
        // ... listing filtered by category ...
       // use 'category' => $catPath,
    }else{
        // ... unfiltered listing ...
    }

}

Thanks for this Drew. I have it all working now as I would like.
However, I have one further question.

I am doing a check on the details page if it exists and throwing a 404 error if not with this code.

if (perch_get('story_slug')) {
    $result = perch_content_custom('Stories', [
        'template'          => 'stories/story',
        'filter'            => 'story_slug',
        'match'             => 'eq',
        'value'             => perch_get('story_slug'),
        'count'             => 1,
        'skip-template'     => true,
        'return-html'       => true,
    ]);

    //This Code redirects to a 404 error if there is no result
    // TODO: CHECK THIS AND MAKE IT WORK
    if(count($result) < 2) {
        http_response_code(404);
        header('Location:'.PerchSystem::get_var('domain').'/404/');
        exit;
    }
}

This works fine. But how can i do the same for the Categories?

For example if I try any URL like;
stories/diagnosis/acute-lymphoblastic-leukaemiasssssss/ or even stories/test/testing

then I get the page back but without any content. Ideally I would like to 404 any incorrect category URLS.

Thanks, Terry

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can use perch_category() to test if the category exists.