Forum

Thread tagged as: Question

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).

Stephen Turvey

Stephen Turvey 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Are you using a page attribute to set the country for a page?

Yes I am.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can use the perch_page_attribute() function to get the value of your attribute.

perch_page_attribute('country', [], true);

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:

<body class="<?php
    perch_page_attributes(array(
        'template' => 'body.html'
    ));
?>">

/pages/attributes/body.html:

<perch:pages id="bodyclass" label="Body classes" help="Webmaster only" type="text" divider-before="Body classes" /> <perch:pages id="country" label="Country" type="select" options="Please select|, Argentina, Cambodia, China, Colombia, Ecuador, Hong Kong, India, Japan, Korea, Myanmar, Nepal, Peru, Thailand, Taiwan, Uruguay, Vietnam" />
Drew McLellan

Drew McLellan 2638 points
Perch Support

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?

Drew McLellan

Drew McLellan 2638 points
Perch Support

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

<meta name="description" content="<perch:pages id="description" label="Description" type="textarea" size="xs" escape="true" count="chars" />" />
<meta name="keywords" content="<perch:pages id="keywords" label="Keywords" type="textarea" size="xs" escape="true" help="Separate with commas" count="chars" />" />

<meta name="robots" content="<perch:pages id="noindex" label="Do not index" type="checkbox" value="noindex" append="," divider-before="Search engine indexing" /><perch:pages id="nofollow" label="Do not follow links" type="checkbox" value="nofollow" append="," /><perch:pages id="nosnippet" label="Do not show a snippet" type="checkbox" value="nosnippet" append="," />" />

body.html

<perch:pages id="bodyclass" label="Body classes" help="Webmaster only" type="text" divider-before="Body classes" /> <perch:pages id="country" label="Country" type="select" options="Please select|, Argentina, Cambodia, China, Colombia, Ecuador, Hong Kong, India, Japan, Korea, Myanmar, Nepal, Peru, Thailand, Taiwan, Uruguay, Vietnam" />

Just figured it out.

default.html

<perch:template path="pages/attributes/seo.html" />
<perch:template path="pages/attributes/body.html" />