Thread tagged as: Question, Meta

Use conditionals for page information within a layout?

I have a global header that I use on all of the pages of my site. Page variables are used to display the titles and description metadata for each page.

However, we've decided that some pages will not have a page description. Is there an easy way to use conditional within a layout template so that the empty meta description tag is not shown? I'd prefer for there to be no meta tag instead of a tag that is empty.

Here is my code below:

<title><?php perch_pages_title(); ?> | Small Wonders Doula</title>
<meta name="description=" content="<?php perch_page_attribute('description'); ?>">
Justin Kaiser

Justin Kaiser 0 points

  • 7 years ago
<perch:if exists="description">
     <meta name="description=" content="<?php perch_page_attribute('description'); ?>">

And take a look at this post from yesterday -

Graham Street, thanks for the assist. I tried your code above several times. For some reason this isn't working, though. It seems like it's processing the conditional as "false" in all cases because the description isn't showing on any page, even the ones that do have a description written for them.

Is your code on the page or in a template? The <perch:if> only works in a template.

That's because there is PHP in a template on that example

<perch:if exists="description">
       <meta name="description" content="<perch:pages id="description" label="Description" type="textarea" size="s" markdown="true" editor="markitup" />">

My code is in a template (i.e. Perch > templates > layouts > global.ducument_header.php).

I bring it into each page on the site with:

  <?php perch_layout('global.document_header'); ?>

Code for the global.document_header.php layout template is as follows:

<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title><?php perch_pages_title(); ?> | Small Wonders Doula</title>
<meta name="description=" content="<?php perch_page_attribute('description'); ?>">
<!-- Standard Favicon -->
<link rel="icon" href="/favicon.ico" />

<!-- For iPhone Retina display: -->
<link rel="apple-touch-icon-precomposed" sizes="120x120" href="/assets/icons/apple-touch-icon-120x120-precomposed.png">

<!-- For iPad Retina: -->
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="/assets/icons/apple-touch-icon-152x152-precomposed.png">

<!-- For iPad Standard Displays: -->
<link rel="apple-touch-icon-precomposed" href="/assets/icons/apple-touch-icon-76x76-precomposed.png">

<!-- For iPhone Standard Displays: -->
<link rel="apple-touch-icon-precomposed" href="/assets/icons/apple-touch-icon-60x60-precomposed.png">

<link href="assets/css/app.min.css" rel="stylesheet" type="text/css" media="all"/>
<link href="assets/css/slick.min.css" rel="stylesheet" type="text/css" media="all"/>

<script src="assets/js/vendor/modernizr.min.js"></script>

As Dexter says, you have PHP in the template and I'm fairly sure you can't do that.

I would tackle it by having your global.header containing everything but the Title & Meta Description then on each page have

<?php perch_page_attributes(); ?>

Then use the methods discussed by Rachel & Graham yesterday:

Drew McLellan

Drew McLellan 2638 points
Perch Support

I would do this:

    $description = perch_page_attribute('description', array(), true);
    if ($description) { 
<meta name="description=" content="<?php perch_page_attribute('description'); ?>">
<?php } // if description ?>

Drew, your solution worked flawlessly (as usual). Thanks again mate.