# Converting related content code to a shortcode

**URL:** <https://discourse.gohugo.io/t/converting-related-content-code-to-a-shortcode/56660>\
**Category:** support\
**Created:** [February 1, 2026, 3:45pm UTC](https://discourse.gohugo.io/t/converting-related-content-code-to-a-shortcode/56660 "2026-02-01T15:45:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![voyajer](https://avatars.discourse-cdn.com/v4/letter/v/eada6e/32.png) [@voyajer](https://discourse.gohugo.io/u/voyajer)\
**Post date:** [February 1, 2026, 3:45pm UTC](https://discourse.gohugo.io/t/converting-related-content-code-to-a-shortcode/56660/1 "2026-02-01T15:45:30Z")

</div>

Hello, I use:

```go
{{ with site.RegularPages.Related . | first 5 }}

<p>Related content:</p>
  <ul>
	 {{ range . }}
	   <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
	 {{ end }}
  </ul>
{{ end }}

```

successfully without errors in my page.html template.

So that I could use the same code in only some pages, I created a shortcode and put the same code in it and get errors:

execute of template failed: template: \_shortcodes/related-pages-shortcode.html:1:13: executing “\_shortcodes/related-pages-shortcode.html” at \<site.RegularPages.Related\>: error calling Related: invalid argument type \*hugolib.ShortcodeWithPage

I have spent hours and hours trying to debug this. I have asked AI for help, it can often be very helpful but in this case makes suggested changes, which still cause errors, and then makes more changes which still causes errors, over and over.

Any help on this would be much appreciated. Thanks ahead of time for any assistance.

---

<div class="post-metadata">

**Author:** ![kijana](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kijana](https://discourse.gohugo.io/u/kijana)\
**Post date:** [February 1, 2026, 4:11pm UTC](https://discourse.gohugo.io/t/converting-related-content-code-to-a-shortcode/56660/2 "2026-02-01T16:11:04Z")

</div>

> [@voyajer](#):
>
> `{{ with site.RegularPages.Related . | first 5 }}`

Haven’t tested, but with Hugo shortcodes, use `.Page` or `$.Page` to pass context instead of `site`.

---

<div class="post-metadata">

**Author:** ![irkode](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/irkode/32/26135_2.png) [@irkode](https://discourse.gohugo.io/u/irkode)\
**Post date:** [February 1, 2026, 4:14pm UTC](https://discourse.gohugo.io/t/converting-related-content-code-to-a-shortcode/56660/3 "2026-02-01T16:14:52Z")

</div>

the site call is not the problem. site is a global.

it’s the dot context that point to the shortcode use `.Page` instead.

> [@voyajer](#):
>
> {{ with site.RegularPages.Related .Page | first 5 }}

> **[Page](https://gohugo.io/methods/shortcode/page/)**
>
> Returns the Page object from which the shortcode was called.

---

<div class="post-metadata">

**Author:** ![voyajer](https://avatars.discourse-cdn.com/v4/letter/v/eada6e/32.png) [@voyajer](https://discourse.gohugo.io/u/voyajer)\
**Post date:** [February 1, 2026, 4:30pm UTC](https://discourse.gohugo.io/t/converting-related-content-code-to-a-shortcode/56660/4 "2026-02-01T16:30:10Z")

</div>

@kijana Thank you for your quick reply. I tried

{{ with .Page.RegularPages.Related . | first 5 }}

and it did not give an error but did not display any related content.

---

<div class="post-metadata">

**Author:** ![voyajer](https://avatars.discourse-cdn.com/v4/letter/v/eada6e/32.png) [@voyajer](https://discourse.gohugo.io/u/voyajer)\
**Post date:** [February 1, 2026, 4:33pm UTC](https://discourse.gohugo.io/t/converting-related-content-code-to-a-shortcode/56660/5 "2026-02-01T16:33:06Z")

</div>

@irkode Thanks very much for your quick reply and providing the solution 🙂

Using:

{{ with site.RegularPages.Related .Page | first 5 }}

works great. Much appreciated.

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [February 3, 2026, 4:34pm UTC](https://discourse.gohugo.io/t/converting-related-content-code-to-a-shortcode/56660/6 "2026-02-03T16:34:01Z")

</div>

Closed on February 3, 2026, 4:34pm UTC

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