Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Only use canonical tag when page is set to index

Solved by Thorsten View solution

Started by Thorsten 2 years ago · 8 replies · 369 views
2 years ago

Hi everyone,

I'm looking for a solution that shows the canonical meta tag only when a page is set to 'index' in the meta tag robots.

Just to clarify things: I use 'robots' = 'index' or 'noindex' on every page as a meta tag to instruct search engines. However since it's SEO best practice, I only want to show the canonical meta tag when a 'robots' meta tag is available on the page AND contains the value 'index'.

Can anyone help me? Any help is much appreciated.

Currently I'm using:

TWIG
 <link rel="canonical" href="{{ page.header.routes.canonical ? page.routeCanonical : page.url(true, true ) }}" />

to generate the canonical tag.

Best,
Thorsten

last edited 03/17/24 by Thorsten
2 years ago

@Thorst3n, Please format your post correctly. Code snippets are not shown without proper use of backticks or triple backticks.

Also please explain what you are trying to achieve.

2 years ago

Hi pamtbaau,

Many thanks for your feedback. I didn't check my post, as I was in a rush. Sorry for that!!!

I worked on the initial post again to clarify my problem and make it easier to understand. I hope this helps.

Best

2 years ago

Hi

I use ‘robots’ = ‘index’ or ‘noindex’

you did not specify how you doing this (I will use select field in blueprint with 4 options index, follow / index, nofollow / noindex, follow / noindex, nofollow ) and set something like:

TWIG
{% if header.index == indexfollow %}<link rel="canonical" href="{{ page.url(true) }}">{% endif %}

hope this will help

2 years ago

my mistake - you said that U use meta tag... but U should easily fit my code with your settings

2 years ago

Hi domena.online,

Many thanks for your help. This makes total sense. But since I'm new to Grav (which seems to be the reason...) I can't get a blueprint to work... It drives me nuts. I use the template resume:
https://github.com/getgrav/grav-theme-resume

On top of that I have a multi language page, so my user/pages folder looks like this:

  • left (folder)
    resume.de.md
    resume.en.md
    personal statement (folder)
    default.de.md
    default.en.md
    languages (folder)
    languages.de.md
    languages.en.md

  • right
    default.de.md
    default.en.md
    experience (folder)
    default.de.md
    default.en.md
    recognitions (folder)
    default.de.md
    default.en.md

  • root.de.md

  • root.en.md

In the users/themes/resume/blueprints folder I have a resume.de.yaml file but it won't change anything in the backend (at least I can't find any changes). I even copied the first example code from here, just to make sure that it would work:
https://learn.getgrav.org/17/forms/blueprints/example-page-blueprint

PS: I also created a left folder in the blueprints folder and added a resume.de.yaml file in there but that wouldn't change anything either.
PS 2: The left folder is the homepage if you will.

Do you think you could help me here once more?

Best,
Thorsten

2 years ago

ah and one more thing. How do I check what value "header.index" currently has?

2 years ago

@Thorst3n,

I have a resume.de.yaml file but it won’t change anything [..]

Don't use language extensions like .de.yaml to blueprints.
The field's label and help properties will automatically be translated when using a proper
language.yaml* file.

I use ‘robots’ = ‘index’ or ‘noindex’ on every page as a meta tag

Do you mean the following inside the header (aka. frontmatter) of the page?

YAML
metadata:
  robots: index

If so, I also presume you are using field 'Metadata' in Admin on tab 'Options'. In that case, there is no need for a blueprint.

Considering my presumptions the following should work:

  • Create a child theme based on Resume
  • Override Resume's template templates/partials/base.html.twig by following the instructions in Extend base template of inherited theme.
    Replace the onTwigLoader function with:
    PHP
    public function onTwigLoader() {
    $resume_path = Grav::instance()['locator']->findResource('themes://resume');
    $this->grav['twig']->addPath($resume_path . DIRECTORY_SEPARATOR . 'templates', 'resume');
    }
    
  • Add the following inside themes/myresume/templates/partials/base.html.twig

    TWIG
    {% extends '@resume/partials/base.html.twig' %}
    
    {% block head %}   
    {{ parent() }}
    
    {% if page.header.metadata.robots == 'index' %}
        <link rel="canonical" href="{{ page.header.routes.canonical ? page.routeCanonical : page.url(true, true ) }}" />
    {% endif %}
    {% endblock %}
    
2 years ago Solution

Hi guys,

Sorry for answering so late, I wasn't working on my page for the last couple of days. However, I got it finaly fixed.

I had to use:

TWIG
{% if site.metadata.robots == 'index' and page.header.metadata.robots != 'noindex' %}
  <link rel="canonical" href="{{ page.url(true) }}" />
  {% include 'partials/langswitcher.hreflang.html.twig' %}
{% endif %}

This is, because I was always looking for a "general" solution for my website.

The main issue was that I'm new to Grav and twig and had to think every through. But your solutions really helped. 🙂

Many thanks!

👍 1
last edited 03/24/24 by pamtbaau

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 74 6 hours ago
General · by pamtbaau, 11 hours ago
1 47 11 hours ago
General · by Andy Miller, 23 hours ago
0 43 23 hours ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 39 5 days ago