Forum

Thread tagged as: Question

Is it possible to check a category is present in two templates?

Hi

I would like to check which category is selected from a content template on a page and then display a template based on a different shared content region which has the same category selected.

Is this possible?

Thanks

Neil

Neil Irwin

Neil Irwin 1 points

  • 3 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Neil,

I would like to check which category is selected from a content template on a page

So you have region (A) on a page and would like to check whether a specific category is selected in it? Yes, this can be done.

then display a template based on a different shared content region which has the same category selected.

I'm not sure I quite understand this part of your question. Do you only want to display the shared region if both the shared region and region (A) have a specific category selected?

Hi Hussein

Thanks for your reply.

I have a page template which creates a content region automatically on adding a new page. Within the the content region there is a category field. The same category set is used on a shared region. I would like to check which category has been selected in the content region associated with that page and then display associated items from the shared region on the page.

Hopefully this makes sense.

Thanks

Neil

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

You can check what category has been selected in PHP by using perch_content_custom with the skip-template option:

$region = perch_content_custom('My Region', [
'skip-template' => true
]);

// check the contents of $region for the category

Alternatively if you are more comfortable working with templates than PHP, you can create a separate template to perform the check:

<perch:categories id="categories"><perch:category id="catPath" /></perch:categories>

Make sure the template has no unnecessary white space. Return the template instead of echoing it:

$catPath = perch_content_custom('My Region', [
'template' => 'category_check.html'
], true);

If you want to compare the catPath inside the shared region template, you can pass it as a variable:

PerchSystem::set_var('catPath', $catPath);
perch_content_custom('Shared');

Or if your shared region is a multi-item region and can be filtered with the category option:

perch_content_custom('Shared', [
'category' => $catPath,
]);

Thanks Hussein for your help on this one.

<?php 
$region = perch_content_custom('Your Region Name', [
  'skip-template' => true
]);

if($region && isset($region[0]['YOUR FIELD ID']) && isset($region[0]['YOUR FIELD ID'][0])) {
  $catPath = $region[0]['YOUR FIELD ID'][0];
} else {
  $catPath = false;
}

perch_content_custom('Your Shared Multi Item Region Name', array(
  'template' => 'your template.html',
  'category' => $catPath,
));
?>

Thanks

Neil