Forum

Thread tagged as: Question, Addons

Get category name from slug

I have a page that's filtering a few projects based on a couple of categories. Built it using the listing/details and filter by category solutions.

When a filter is active and I've got a slug set in the url (for use with content_custom and perch_get), is there a way to fetch the category name based on the slug so I can clarify for the user which category is active at the moment?

Jonas Edvardsson

Jonas Edvardsson 0 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You can use perch_category() to get everything back for a category. Pass it the category path, e.g.

perch_category('colour/blue', [
    'template' => 'title.html',
]);

Ah, got it to work using a template.

Also tried using skip-template and raw to get the category info into an array for easier handling but that wouldn't do quite well. Adding true after the options array while supplying a template worked but is this the way to go?

$path = 'hustyper/' . perch_get('kategori');

$cat = perch_category($path, [
  'template' => '_catTitle.html'
], true);

print $cat;
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, that should do it.