Forum

Thread tagged as: Question, Problem, Meta

Automatic page title tags

Hi guys, apologies if there's already an answer for this somewhere or if I'm missing something blindingly obvious!

I've got a single global header.php file which contains all of the <head> content and main navigation which is the same across every page. Ideally I need to have the <title> tag auto populate with the page title for all the different sections. I'm running the blog app and have several pages. Here's my <title> tag code at the moment:

<title>
        <?php perch_pages_title(); ?>
        <?php perch_blog_post_field(perch_get('s'), 'postTitle'); ?>
         | Website Title
</title>

This seems to work fine for my pages and blog posts, although it's probably not the most elegant way of doing it. The issue I've got now though is that I followed the portfolio site video tutorial (https://docs.grabaperch.com/video/v/portfolio-with-categories/) to create a Work section on this site. Now because the portfolio items are child content of the Work page, I can't get a portfolio item to display its own title, but I can get them to show the parent page title for all but that's not ideal for SEO.

I can get it to output the parent page title (Work) using perch_page_attribute however this can't dynamically change between different portfolio items.

Is there a way to set up a single header.php file to create <title> content automatically for pages, blog and list / item setups?

Alan Charlesworth

Alan Charlesworth 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I would use layout variables to pass the title in from the page if you want to use something other than the default perch_pages_title().

If a custom title has been set, output that in your header. If not, use perch_pages_title() as usual.

Hi Drew, thanks for your reply. You sent me on the right track and I found this thread - https://forum.grabaperch.com/forum/08-18-2015-title-and-meta-tags-for-listdetail-setup - which is exactly what I'm trying to do. I've managed to get Louise's first solution working but that requires another header.php layout which is what I'm doing in the short term.

The second part of that solution offered by Keir is ideally what I'm trying to achieve, however I can't seem to get it to work. I've got a custom field set on my detail (work) items so the content editor can enter a title and I can get it to return with perch_content_custom but can't get it working with layout variables.

I'm probably missing something obvious but can't for the life of me see it! I've used Keir's code exactly as he put it on the other thread, only changing the Custom Content region to mine (Case Studies) and the field to collect content from (page-title).

case-studies.php:

<?php
// PROJECT DETAIL SEO
// get project detail data
$content = perch_content_custom('Case Studies', array(
'filter' => 'slug',
'match' => 'eq',
'value' => perch_get('s'),
'count' => 1,
'skip-template' => 'true',
));

// header include (with overrides)
perch_layout('global/header',
[ 'page-title' => $content[0]['page-title'],
]);
?>

global.header.php:

<title>
<?php
// title overrides if (perch_layout_has('page-title')) {
perch_layout_var('page-title');
} else {
perch_pages_title();
}
?>
</title>

It outputs the perch_pages_title(); fine but won't get the content from the page-title field.

I've got something that works for now but ideally I'd like to get it working this way as it's the better way to do it. Any ideas what I'm missing?

Thanks

Drew McLellan

Drew McLellan 2638 points
Perch Support

In your detail page, what do you get if you do this?

print_r($content[0]);