If post tagged "MyTag" exists, show a link to the latest post

I’m using the shortcode layouts/_default/shortcodes/mytag-recent-post.html in posts:

<!-- mytag-recent-post -->

{{ range first 1 .Site.Taxonomies.tags.mytag }}
<a href="{{ .Page.RelPermalink }}">{{ .Page.Title }}</a>
{{ end }}

And that {{< mytag-recent-post >}} shows the link to the latest post tagged “MyTag.”

But if there are no existing posts tagged “MyTag”, I get "error calling first: both limit and seq must be provided.”

How can I first determine if there are any existing posts tagged MyTag, and if there is one, show the link, but if there are none, show no link?

{{ with site.Taxonomies.tags.mytag }}
  {{ with index . 0 }}
    <a href="{{ .RelPermalink }}">{{ .Title }}</a>
  {{ end }}
{{ end }}

or

{{ with site.Taxonomies.tags.mytag }}
  {{ range first 1 . }}
    <a href="{{ .RelPermalink }}">{{ .Title }}</a>
  {{ end }}
{{ end }}

Thanks! That works.

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