Forum
Setting variable and passing into perch_content_custom
I have a template called "country" and I'd like to set and then pass in a variable in order to show some items related to each specific country page using that template. Here's the template so far:
<?php
include($_SERVER['DOCUMENT_ROOT'].'/admin/runtime.php');
perch_content_create('Page header', array(
'template' => 'page_header.html',
'multiple' => false,
));
perch_content_create('Content', array(
'template' => 'programs/freeform.html',
'multiple' => true,
));
perch_layout('header', array(
'title' => perch_pages_title(true),
));
?>
<?php
perch_pages_parent_page(array(
'hide-extensions' => true,
'hide-default-doc' => true,
'template' => 'ancestor.html',
));
?>
<div class="clear page-header ta-c mb-0">
<div class="wrap wrap--large">
<h1><?php perch_pages_title(); ?></h1>
<?php perch_content('Page header'); ?>
</div>
</div>
<?php perch_content('Content'); ?>
<div class="clear feature-block">
<div class="wrap wrap--large">
<?php
perch_content_custom('Program overview', array(
'page' => '/countries/*',
'template' => 'programs/program_teaser_overview.html',
'count' => 10,
'filter'=>'overview_country',
'match'=>'eq',
'value'=>'Colombia',
'sort'=>'overview_country',
'sort-order'=>'ASC',
));
?>
</div>
</div>
<?php perch_layout('footer'); ?>
This works for Colombia but I want it to be dynamic, so instead of 'value'=>'Colombia' I want something like 'value'=>$country then at the top of the template somehow set that variable to whatever country the page is about (not sure how to get that value).
Are you using a page attribute to set the country for a page?
Yes I am.
You can use the
perch_page_attribute()
function to get the value of your attribute.https://docs.grabaperch.com/functions/page-attributes/perch-page-attribute/
I got that to work but I then tried to move the code into a different attributes template in order to output the value as a body class (along with a free text field for specifying body classes) and it's not working anymore. Debug shows that it has found the template but nothing appears under Page Details in admin.
header.php layout file snippet:
/pages/attributes/body.html:
Is the page set to use that attribute template?
Not sure I follow. I thought if I had the perch_page_attributes in my header layout file then all pages using that layout would be able to use the attribute?
Nope. Each page has a master attribute assigned to it in the page options.
Right. So how do I have multiple attributes enabled for a page? For example I've got two templates at the moment, one to assign body classes and another for SEO. But I can only select one template in the Page Options?
seo.html
body.html
Just figured it out.
default.html