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.

Support

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

Solved by pamtbaau View solution

Started by rappluk 8 months ago · 4 replies · 44 views
8 months 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

8 months 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 ?

8 months 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?

8 months 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
---

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 50 6 hours ago
Support · by Anna, 2 days ago
2 58 9 hours ago
Support · by Justin Young, 10 hours ago
1 28 10 hours ago
Support · by Duc , 1 week ago
2 63 5 days ago
Support · by Colin Hume, 1 week ago
2 53 5 days ago