[SOLVED] Does Related Content Not Work in Shortcodes?


<div class="related">
    {{ $related := .Site.RegularPages.Related . | first 2 }}
    {{ with $related }}
    <div class="row">
        {{ range . }}
        <div class="col-md-3 col-sm-6">
            <div class="box-image-text shadow" style="height: 280px;">
                <div class="top">
                    <div class="image" style="overflow:hidden">

                        <a href="{{ .RelPermalink }}">
                        {{ if isset .Params "banner" }}
                        <img src="{{ .Site.BaseURL}}img/placeholder.png" data-src="{{ .Site.BaseURL}}{{ .Params.banner }}" class="img-responsive shadow" alt="{{.Title}}" width="255" height="192" title="{{.Title}}">
                        {{ else }}
                            <img src="{{ .Site.BaseURL}}img/placeholder.png" class="img-responsive" alt="{{.Title}}" width="255" height="192" title="{{.Title}}">
                        {{ end }}
                        <h4>{{ .Title }}<h4>
        {{ end }}
    {{ end }}

Markdown Content calling shortcode:

{{< related >}}

Hugo build error:

shortcodes/related.html:2:46: executing "shortcodes/related.html" at <.>: wrong type for value; expected related.Document; got *hugolib.ShortcodeWithPage
Total in 784 ms
Error: Error building site: logged 1 error(s)

I am not understanding why there is the wrong type for the value. What is the type for this? And how can I give the correct type?

You need to do:

{{ $related := .Site.RegularPages.Related .Page | first 2 }}

Thanks for reply, @bep

I am now using:

{{ $related := .Site.RegularPages.Related .Page | first 2 }}

And now the error Hugo gives is this:

shortcodes/related.html:2:24: executing "shortcodes/related.html" at <.Site.RegularPages.R...>: error calling Related: *hugolib.PageWithoutContent not supported
Total in 792 ms
Error: Error building site: logged 1 error(s)

Dang. That is a bug. Can you create a GitHub issue and I will fix it for a release in a few days?

1 Like

Yes sir, absolutely. Done!

Please check: https://github.com/gohugoio/hugo/issues/5071