Skip to content
● Critical PHP security fixes are out: update PHP on your Grav sites. What to do →

How to link to page-specific target page within my theme

Solved by pamtbaau View solution

Started by rappluk 1 year ago · 4 replies · 400 views
1 year ago

I am creating a theme which applies a floating button to every page, which should link to the /contact page in this case
How would I go about ensuring that the proper link gets put into the anchor tag in my template? Taking into account localized pages, etc.

I was thinking to put a variable “buttontarget” into each page’s frontmatter, so the twig can construct the proper localized url out of that.
So what should the buttontarget actually contain to identify the page? (the folder name of the page02.contact, just /contact/myform as a full path, or myform, the slug)
and how do i transform whatever the variable contains to a working page link in twig?
again, i need the language routing to still apply for that

1 year ago

why not

HTML
<a href="https://mywebsite.com/contact"> </a>

?

do i miss something?

@rappluk:
i need the language routing to still apply for that

does this mean you want multi-lang website and /contact will be something like /br/contact ?

1 year ago

Well I might want to change the button content link (or hide it on the contact page itself) so I want to work with it somehow being tied to the page it is on.

The localized link might be /contact for english, /kontakt for german and something entirely different for other languages. so just getting language and constructing the link myself probably wont work?

1 year ago Solution

@rappluk,

I'm afraid that <a href="https://mywebsite.com/contact"> </a> will always go to the contact page using the default language. It also won't translate the link.

See if this Twig snippet works for you:

TWIG
{% set contactPage = pages.find('/contact') %}
<a href="{{ contactPage.url }}">{{ contactPage.title }}</a>

In combination with frontends like:
German

YAML
---
title: Kontakt DE
slug: kontaktDE
---

English:

YAML
---
title: Contact EN
slug: contactEN
---
1 year ago

Worked like a charm, thank you 🙂

Suggested topics

Topic Participants Replies Views Activity
Support · by webmasterodp, 2 weeks ago
4 248 2 weeks ago
Support · by nwhitfield, 2 weeks ago
2 213 2 weeks ago
Support · by stell, 4 weeks ago
5 458 3 weeks ago
Support · by gravinator, 2 months ago
5 637 3 weeks ago
Support · by Gravver, 1 month ago
6 430 4 weeks ago