Forum

Thread tagged as: Question

Using Layout Variables for blog meta

Hello,

I'm using Layout Variables to take care of the meta information on blog posts.

I have the following in my post.html template:

<perch:blog id="postTitle" type="text" label="Title" required="true" size="xl autowidth" order="1" />
 <perch:blog id="meta_description" type="textarea" label="Meta Description" suppress="true" size="xs" />
 <perch:blog id="meta_keywords" type="textarea" label="Meta Keywords" suppress="true" size="xs" />

In my post.php

<?php perch_layout('global.above', array(
    'title'  => perch_blog_post_field(perch_get('s'), 'postTitle', true),
    'meta'  => perch_blog_post_field(perch_get('s'), 'meta_description', true), 
    'keywords'  => perch_blog_post_field(perch_get('s'), 'meta_keywords', true),

    ));
?>

In global.above.php


<title><?php perch_layout_var('title'); ?></title> <?php if (!perch_layout_var('page_att', true)) { echo "<meta name='description' content='"; perch_layout_var('meta'); echo "' />"; echo "<meta name='keywords' content='"; perch_layout_var('keywords'); echo "' />"; } ?>

The Post Title is working, but not the keywords or description. I've removed 'suppress="true"' from the post.html template to make sure they are working ok there and they are.

Sarah Evans

Sarah Evans 0 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

What does this output?

perch_blog_post_field(perch_get('s'), 'meta_description');

'Array'

Drew McLellan

Drew McLellan 2638 points
Perch Support

Oh! Interesting. That sounds like a bug to me. I'll get onto it.

In the meantime, it would be more efficient to make a template containing your meta tags:

<title><perch:blog id="postTitle" type="text" label="Title" required="true" size="xl auto width" order="1" /></title>
<meta name="description" content="<perch:blog id="meta_description" type="textarea" label="Meta Description" suppress="true" size="xs" escape="true" />" />
<meta name="keywords" content="<perch:blog id="meta_keywords" type="textarea" label="Meta Keywords" suppress="true" size="xs" />" />

and then

<?php perch_layout('global.above', array(
    'meta'  => perch_blog_custom(array(
            'template' => 'blog_meta.html',
            'filter' => 'postSlug',
            'match' => 'eq',
            'value' => perch_get('s'),
        ), true), 

    ));
?>

and

<?php 
if (perch_layout_var('meta', true)) {
    perch_layout_var('meta');
} 
?>

Fewer database queries, so your site will use less resources and load faster.

Thanks for coming back to me on this Drew.

I don't quite follow.

If I have a separate template file for the meta which isn't part of the post.html where can you fill in these details without a region?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Not an separate template, an additional template. Keep them in your post.html. Duplicate them in the new template for display.

Thanks for the explanation Drew.

Oddly it's now displaying the this meta template code inside the body (where the blog custom was placed inside post.php) inside quotes.

It's also just giving me the post title information and not the description and keywords like my previously.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, show me what you've got.

post.html:


<article class="h-entry"> <h1> <perch:blog id="postTitle" type="text" label="Title" required="true" size="xl autowidth" order="1" /> </h1> <p class="meta"> <perch:categories id="categories" set="blog" label="Categories" display-as="checkboxes"> Category: <a href="archive.php?cat=<perch:category id="catSlug" type="slug" />" class="p-category"> <perch:category id="catTitle" type="text" /></a> </perch:categories> <time class="dt-published date" datetime="<perch:blog id="postDateTime" type="date" label="Date" time="true" format="Y-m-d H:i:s" divider-before="Meta information" />"> <perch:blog id="postDateTime" type="date" time="true" format="%d %B %Y" /> </time> </p> <div class="description e-content"> <perch:blog id="postDescHTML" type="textarea" label="Post" order="2" editor="markitup" markdown="true" imagewidth="800" size="xxl autowidth" required="true" /> </div> <meta name="description" content="<perch:blog id="meta_description" type="textarea" label="Meta Description" suppress="true" size="xs" escape="true" />" /> <meta name="keywords" content="<perch:blog id="meta_keywords" type="textarea" label="Meta Keywords" suppress="true" size="xs" />" /> </article>

blog_meta.html

<title><perch:blog id="postTitle" type="text" label="Title" required="true" size="xl auto width" order="1" /></title>
<meta name="description" content="<perch:blog id="meta_description" type="textarea" label="Meta Description" suppress="true" size="xs" escape="true" />" />
<meta name="keywords" content="<perch:blog id="meta_keywords" type="textarea" label="Meta Keywords" suppress="true" size="xs" />" />

post.php

<?php include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php');?>

<?php perch_layout('global.above', array(
    'meta'  => perch_blog_custom(array(
            'template' => 'blog_meta.html',
            'filter' => 'postSlug',
            'match' => 'eq',
            'value' => perch_get('s'),
        ), true), 

    ));
?>


<div class="grid-container">

    <div class="content-container inner">

        <div class="grid-row">

            <main class="col-16">

                <?php perch_blog_post(perch_get('s')); ?>

                <a class="highlight-link highlight-link--back" href="/blog">Back to Blog</a>
[...]

global.above (I know this currently would give me two <title> elements for posts)

<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="utf-8">
<title><?php perch_layout_var('title'); ?></title>

<?php 
if (perch_layout_var('meta', true)) {
    perch_layout_var('meta');
} 
?>

  <?php if (perch_layout_var('page_att', true)) {
    echo perch_page_attributes();
    }
  ?>


<link rel="stylesheet" href="/css/styles.css">

[...]

Drew McLellan

Drew McLellan 2638 points
Perch Support

You need to remove the suppress="true" from blog_meta.html.

Ah super, these are coming through now, thank you :)

Any thoughts to why this is appearing inside the posts body rather than in the head where it's been placed in the global.above?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Nope - where is it appearing?

After some investigation the issue is the html from the blog_meta.html template is coming through with entitles:

&lt;title&gt;Test Post&lt;/title&gt;
&lt;meta name="description" content="test" /&gt;
&lt;meta name="keywords" content="test" /&gt;

Drew McLellan

Drew McLellan 2638 points
Perch Support

Change

perch_layout_var('meta');

to

echo perch_layout_var('meta', true);

Thanks Drew