Forum

Thread tagged as: Problem, Blog

Blog seo template problems

Not quite sure what is going on but I have created some seo header templates for my blog but for some reason it repeats 3 times in each header (no matter if it is the blog home page, an archive page or a post page). For some reason the information for the archive pages isn't loading either (i.e. the category Title).

It's probably better if I show the code:

homepage_header.html

<title>Website, Digital Marketing &amp; Business Articles - The Web Bakery Blog</title>
<link rel='canonical' href="https://thewebbakery.co.uk/blog">
<link rel="publisher" href="https://plus.google.com/107682658435138912323">
<meta property="og:locale" content="en_GB">
<meta property="og:type" content="website">
<meta property="og:title" content="Website, Digital Marketing &amp; Business Articles - The Web Bakery Blog">
<meta property="og:url" content="https://thewebbakery.co.uk/blog">
<meta property="og:site_name" content="The Web Bakery Blog">
.
.
.

category_header.html

<title><perch:blog id="categoryTitle" /> Archives - The Web Bakery Blog</title>
<link rel='canonical' href="https://thewebbakery/blog/topic/<perch:blog id="categroySlug" />">
<link rel="publisher" href="https://plus.google.com/107682658435138912323">
<meta property="og:locale" content="en_GB">
<meta property="og:type" content="object">
<meta property="og:title" content="<perch:blog id="categoryTitle" /> Archives - The Web Bakery Blog">
<meta property="og:url" content="https://thewebbakery/blog/topic/<perch:blog id="categorySlug" />">
.
.
.

post_header.html

<title><perch:blog id="postTitle" /></title>
<meta name="description" content="<perch:blog id="postDesc" />">
<link rel='canonical' href="https://thewebbakery.co.uk/blog/article/<perch:blog id="postSlug" />">
<perch:if exists="authorGivenName">
    <link rel="author" content="<perch:blog id="authorGivenName" /> <perch:blog id="authorFamilyName" />">
</perch:if>
<perch:if exists="authorGoogle">
    <link rel="publisher" href="<perch:blog id="authorGoogle" />">
<perch:else />
    <link rel="publisher" href="https://plus.google.com/107682658435138912323">
</perch:if>
<meta property="og:locale" content="en_GB">
<meta property="og:type" content="article">
<meta property="og:title" content="<perch:blog id="postTitle" />">
<meta property="og:description" content="<perch:blog id="postDesc" />">
<meta property="og:url" content="https://thewebbakery.co.uk/blog/article/<perch:blog id="postSlug" />">
.
.
.

blog.header.html

<?php
    require_once($_SERVER['DOCUMENT_ROOT'].'/inc/constants.php');

    $header_template = 'homepage_header.html';

    if (perch_layout_var('is_article', true)) {
        $header_template = 'post_header.html';
    } elseif (perch_layout_var('is_category_archive', true)) {
        $header_template = 'category_header.html';
    } elseif (perch_layout_var('is_tag_archive', true)) {
        $header_template = 'tag_header.html';
    } elseif (perch_layout_var('is_year_archive', true)) {
        $header_template = 'year_header.html';
    } elseif (perch_layout_var('is_author_archive', true)) {
        $header_template = 'author_header.html';
    }
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <?php perch_blog_custom(array('template'=>$header_template)); ?>
        <link rel="stylesheet" href="<?php echo CSS_PATH; ?>/screen.css">
    </head>

The problem is, where I am calling perch_blog_custom(array('template'=>$header_template)); I get the required template html in the header of the page where it should be but it is repeated 3 times. Then on the archive page (using the category archive as the example) there is nothing output (just a blank space) where I am using <perch:blog id="categoryTitle" />

The files are stored in 'perch/templates/blog' as I think they should be.

Any ideas?

Emma Davis

Emma Davis 0 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

perch_blog_custom() will list all your posts unless you filter it somehow.

Oh, of course.

So is there a better way to load up the header seo as I am not loading the posts which is obviously what perch_blog_custom() is for?

Also this is obviously what is stopping things like the category title from displaying as it is based on posts, not archives etc?

Drew McLellan

Drew McLellan 2638 points
Perch Support

No, that's good, just at a filter to find the post you want.

Sorry, I have been out most of the day but what about for the archive and author pages as filtering for a post won't work then?

Drew McLellan

Drew McLellan 2638 points
Perch Support

What output do you need in those cases? Surely not tags for every post?

No, as per the code in my original post (using a category archive as an example) I want to output the name of the category.

i.e. Someone clicks on the category Business to view all posts in that category and I want to be able to populate the page title and og:title with the category name

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, great. You can use perch_blog_category() to get the category title.

Oh, right, thank you - I obviously missed that code somewhere :)

Drew McLellan

Drew McLellan 2638 points
Perch Support

Sorry, but this is still not what I need.

I want to be able to use <perch:blog id="categoryTitle" /> and <perch:blog id="categorySlug" />in the html header template for the category archive page.

Drew McLellan

Drew McLellan 2638 points
Perch Support

There's not just one of those - a blog post can be in multiple categories.

But there is just one of those if I am on a category archive page which is what I am talking about

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, so do you have the category path or slug? If so, you should be able to pass that into the appropriate category page function to get the title.

Whoops, sorry Drew. I have now sorted this by doing something different (one being, I remembered that the slug is passed in the url so I could just use perch_get('cat') to 'get' it :) ).

If anyone is interested, below is how I have resolved my header issues:

On the archive.php page I create an array of variables to pass through to a specific archive header:

   $header_array = array();

if (perch_get('cat')) {
    $header_array['archive_type'] = 'topic';
    $header_array['archive_name'] = perch_blog_category(perch_get('cat'), true);
    $header_array['archive_slug'] = perch_get('cat');
} elseif (perch_get('tag')) {
    $header_array['archive_type'] = 'tag';
    $header_array['archive_name'] = perch_blog_tag(perch_get('tag'), true);
    $header_array['archive_slug'] = perch_get('tag');
} elseif (perch_get('author')) {
    $header_array['archive_type'] = 'author';
    $header_array['archive_name'] = perch_blog_author(perch_get('author'), true);
    $header_array['archive_slug'] = perch_get('author');
}

perch_layout('blog.archive.header', $header_array);

Then in the blog.archive.header.php, I assign the passed data to new variables for echoing out as required:

    $archive_type = perch_layout_var('archive_type', true);
$archive_name = perch_layout_var('archive_name', true);
$archive_slug = perch_layout_var('archive_slug', true);

    <title><?php echo $archive_name; ?> Archives - The Web Bakery Blog</title>
    <link rel='canonical' href="https://thewebbakery/blog/<?php echo $archive_type; ?>/<?php echo $archive_slug; ?>">

As for the post pages, I load perch_layout('blog.article.header'); as normal and use a combination of perch functions and my own PHP to get the data about the article that I want to display:

    $dynamic = perch_blog_post_field(perch_get('s'), 'postDynamicFields', true);
$data = json_decode($dynamic, true);

$meta_description = $data['metaDesc']['raw'];
$image = $data['image'];

$author = perch_blog_author_for_post(perch_get('s'), array('skip-template' => true));

    <title><?php perch_blog_post_field(perch_get('s'), 'postTitle'); ?></title>
<meta name="description" content="<?php echo $meta_description; ?>">
<link rel='canonical' href="https://thewebbakery.co.uk/blog/article/<?php perch_blog_post_field(perch_get('s'), 'postSlug'); ?>">
    <meta property="og:image" content="https://thewebbakery.co.uk/manage/resources/blog-images<?php echo $image['path']; ?>">
    <meta name="twitter:creator" content="@<?php echo $author['authorTwitter']; ?>"/>

That looks useful. Thanks for posting it.

Clive Walker said:

That looks useful. Thanks for posting it.

No problem, always like to help if I can and find a solution to a problem that others may also be having :) Obviously I haven't put all of the meta tags and information on here but hopefully you get the idea.