Shortcode creating card for specific post/content and including frontmatter data

Is it possible to create a shortcode, ie. {{< card "Post Title" >}} or {{< card "post-title" >}} referring a specific post/page bundle but also output some of the data set via frontmatter? I’ve tried searching around but haven’t been able to find anything – would appriciate any help or nudge in the right direction. Closest thing was scripter.co/titleref-referencing-hugo-posts-by-their-titles, but I haven’t been able to modify the code and get any of the frontmatter showing.

Goal: Output a visual card using a shortcode within my markdown content, featuring a specific post/page bundle on the site, that also output related frontmatter data within the card (ie. categories, date etc.). Not to be confused with a ‘Similar posts’ section, as I would like to specify the specific post within the shortcode.

It can be achieved by GetPage function.

{{/* layouts/shortcodes/refcard.html */}}
{{ $page := site.GetPage (.Get 0) }}
{{ with $page }}
  <div>{{ .Title }}</div>
  <ul>
    {{ range .GetTerms "tags" }}
      <li>
        <a href="{{ .RelPermalink }}">{{ .Title }}</a>
      </li>
    {{ end }}
  </ul>
{{ end }}

And then use the shortcode in content.

{{< refcard "/docs/modules/search" >}}

Note, there may be an infinite recursion issue when you’re going to output some page’s stuff (summary, reading time etc) inside a shortcode.

Thank you, exactly what I was looking for!

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