Forum
Categories in page attributes
How should I set a category in page attributes and then use it in a page template?
I've tried this in pages/attributes/default.html:
<perch:categories id="service" label="Which service category does this page relate to?" set="Services" required="true" />
<perch:category id="catTitle" type="text" />
</perch:categories>
<perch:pages id="header_layout" type="select" label="Choose which header layout to display" options="Header layout 1|global.header_layout1, Header layout 2|global.header_layout2" allowempty="false" required="true" />
...
... etc for other attributes
Then, in page template, I'm trying to pick up the category/ies in a variable:
$service_category = perch_page_attribute('service', [], true);
echo $service_category;
This outputs nothing in $service_category.
But, for comparison, if I pull another page attribute into this variable instead in page template:
$service_category = perch_page_attribute('header_layout', [], true);
echo $service_category;
This outputs the value selected for this attribute.
I think the category is being set for the page. So how do I use it in a page template?
Diagnostics
Perch Runway: 2.8.34, PHP: 5.5.12, MySQL: mysqlnd 5.0.11-dev - 20120503 - $Id: bf9ad53b11c9a57efdb1057292d73b928b8c5c77 $, with PDO
Server OS: WINNT, apache2handler
Installed apps: content (2.8.34), assets (2.8.34), categories (2.8.34), collection_3 (2.8.34)
App runtimes: <?php $apps_list = array( 'content', 'categories', );
PERCH_LOGINPATH: /cms
PERCH_PATH: C:\wamp\www\CLIENT\cms
PERCH_CORE: C:\wamp\www\CLIENT\cms\core
PERCH_RESFILEPATH: C:\wamp\www\CLIENT\cms\resources
Image manipulation: GD
PHP limits: Max upload 64M, Max POST 50M, Memory: 1024M, Total max file upload: 50M
F1: 6a33f95eca3667f9e0c39bf5ca2980fe
Resource folder writeable: Yes
HTTP_HOST: CLIENT.local
DOCUMENT_ROOT: C:/wamp/www/CLIENT
REQUEST_URI: /cms/core/settings/diagnostics/
SCRIPT_NAME: /cms/core/settings/diagnostics/index.php
I think you'll need to output them through a template.
Sorry Drew, but how?
I'm now using a page attributes template service_category.html
And pulling this into my page attributes default.html as follows.
I can set a category in Page Details for each page.
In my page template:
But this returns nothing.
I feel I'm using a massive sledgehammer and failing to crack a nut. Not sure whether all the PHP trimming is required, but it does remove the trailing slash.
echo $service_category; outputs what looks like a valid category path eg services/personal-injury. But when I use $service_category to filter my collection, I get nothing.
And if I filter the collection by typing in a category path instead of using the variable, it works.
What am I doing wrong?
If it works when you hard-code the value but not with the variable, the variable doesn't hold the value you think it does.
Have you turned on debug and checked for the category path you think you're filtering with?
Thanks Drew. There was "\r\n" at the start of the value that was interfering. So I changed a rtrim to a trim and everything appears to be working OK now. My final page template code in case it's of use to anyone else is:
Quick question. Is this the simplest way to select a category in page attributes and then use it to filter collections in a page template? I'm a bit surprised I have to fiddle around manipulating the PHP variable this way.
Yes, that seems very convoluted. I think I would just use
skip-template
.Drew I can't see how to use
skip-template
to return thecatPath
The array returned doesn't contain
catPath
: