Link to page anchor only, relative to the current page

Hi,
I’m trying to link to a page anchor defined in my config.toml which results in a relative link.

In my config.toml i have:

[languages.en.params.navigation_button]
enable = true
label = "Contact us"
link = "#contact"

and on my header I have (in the header.html partial of my theme):

    {{ if site.Params.navigation_button.enable }}
    {{ with site.Params.navigation_button }}
    <a href="{{ .link | relURL }}" class="btn btn-sm btn-primary ml-3">{{ .label }}</a>
    {{ end }}
    {{ end }}

However, in the end, no matter which sub-page I am on the contact button will alwas link to $TOPLEVEL.com/#contact and not to, for example "TOPLEVEL.com/about-us#contact.

What could I do to achieve that?

thanks!

In the above construct you are telling Hugo to create a relURL out of the page anchor.
Naturally the relURL points to the host root.

You need to pass the context of the current page, before the scope of site.Params.navigation_button.enable.

Something like: $currentPage := .

And then within the scope of the parameter create the path with something like:
$link := print $currentPage.RelPermalink .link

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.