Forum

Thread tagged as: Question

Filtering categorized content in perch_content_custom

I would like to display all FAQs on a primary FAQs page, but also display select ones on other pages through the site.

To do this I have created a category set, and am trying to filter based on that without:

  1. displaying cat=abc in the URL; or
  2. without having to hard-code the category slug in perch_content_custom

FAQs region on the /more-info page includes the perch:categories tag using the "massage-therapy-techniques" set:

<perch:categories id="techniques" label="Related technique(s)" set="massage-therapy-techniques" help="Set to display on a specific technique page" />

On individual "technique" pages, I call perch_content_custom:

<?php perch_content_custom('FAQs', array(
'template' => 'faqs.html',
'page' => '/more-info.php',
'category' => 'massage-therapy-techniques/systemic-deep-tissue-therapy',
)); ?>

Which works as intended, as long the category => is hard coded, OR if I have a URL like:

example.com/massage-therapy-techniques/systemic-deep-tissue-therapy?cat=massage-therapy-techniques/systemic-deep-tissue-therapy

And I use 'category' => perch_get('cat') in the perch_content_custom call. But that string looks pretty goofy, and hardcoding it isn't practical in this instance.

What I would like is to use:

<?php perch_content_custom('FAQs', array(
'template' => 'faqs.html',
'page' => '/more-info.php',
'category' => perch_get('cat'),
)); ?>

And end up with a URL like:

example.com/massage-therapy-techniques/systemic-deep-tissue-therapy

I suspect this comes down to a rewrite rule of some sort, but I am getting nowhere. Can anyone provide any insight into how this may be done?

Cheers!

Richard Terrick

Richard Terrick 3 points

  • 5 months ago
Hussein Al Hammad

Hussein Al Hammad 67 points
Registered Developer

Hello Richard,

If your pages always include the category path in the URL like example.com/cat/cat, why not get it from there? No URL parameters and no rewrites:

$catPath = perch_page_url([
    'include-domain' => false,
], true);

perch_content_custom('FAQs', array(
    'template' => 'faqs.html',
    'page' => '/more-info.php',
    'category' => $catPath,
));

Hi Hussein,

It’s funny I had the same “I wonder if I could just...” thought after I posted this while I was driving to the store but didn’t know exactly how to realize it. Thanks ever so much! Will test this in the morning.

By the way I’ve really enjoyed and learned a lot from your posts and code on the grabapipit.com site (also thanks to the Perchology newsletter for bringing it to my attention).

Cheers!

Quick follow-up: the code from Hussein above got me on the right track, but a couple extra steps were needed to complete the job for my particular layout.

Posting below for future reference:

// grab the page url, minus the domain and extention
$catPath = perch_page_url([
'include-domain' => false,
'hide-extensions' => true,
], true);

// strip the leading slash to conform with the category path (ex: /set/cat does not work, but set/cat does)
$catStripped = ltrim($catPath,'/');

// populate the category option with the $catStripped variable
perch_content_custom('FAQs', array(
'template' => 'faqs.html',
'page' => '/more-info.php',
'category' => $catStripped,
));

Thanks again Hussein!

Hussein Al Hammad

Hussein Al Hammad 67 points
Registered Developer

Nicely done, Richard. I've been using Runway more recently and at times I forget extensions even exist ^^!

By the way I’ve really enjoyed and learned a lot from your posts and code on the grabapipit.com site (also thanks to the Perchology newsletter for bringing it to my attention).

Thanks. Good to hear this from other developers. Yeah Perchology played a big role in spreading the word (thanks Clive!).