Cross-reference with automatic title

I’d like to refer to another page on my document, and have Hugo automatically insert title of that page, with a href to the document. Can this be done?

I know I can do it myself with [title]({{< relref "doc"}}) but I’d like the title to be automatically the same and automatically updated, as other static site genenerators do.

Can’t find anything about this in the docs.

1 Like

You can create your own shortcode that does this.

Edited to add: You can also use render hook templates.

1 Like

This works for me. Create a shortcode xref.html:

{{ with .Site.GetPage (.Get 0) }}<a href="{{ .RelPermalink }}">{{ .Title }}</a>{{ end }}

Then call it with {{< xref "myTopic" >}}

3 Likes

Perfect. Thanks for sharing. Here is an updated version with error handling for invalid topics:

{{ with .Site.GetPage (.Get 0) }}<a href="{{ .RelPermalink }}">{{ .Title }}</a>{{ else }}
{{ errorf "Unable to find topic" .Get 0 }}
{{ end }}

And for the other newbies like me: this should go into ./layouts/shortcodes/sref.html.