Arif
1
Or can they be condensed further?
{{- $imgURL := "" }}
{{- $width := "" }}
{{- $height := "" }}
{{- $mediaType := ""}}
{{- if and (eq .Params.layout "news") (eq .Kind "page") }}
{{- with .Parent.Resources.GetMatch "thumbnail.*" }}
{{- $imgURL = .Permalink }}
{{- $width = .Width }}
{{- $height = .Height }}
{{- $mediaType = .MediaType.Type }}
{{- end }}
{{- else }}
{{- with or (.Resources.GetMatch "thumbnail.*") (.Resources.GetMatch "thumbnail.fr.*") }}
{{- $imgURL = .Permalink }}
{{- $width = .Width }}
{{- $height = .Height }}
{{- $mediaType = .MediaType.Type }}
{{- end }}
{{- end -}}
Is this a multilingual site? Something likeβ¦
content/
βββ posts/
β βββ post-1/
β β βββ index.fr.md
β β βββ index.md
β β βββ thumbnail.fr.jpg
β β βββ thumbnail.jpg
β βββ _index.fr.md
β βββ _index.md
βββ _index.fr.md
βββ _index.md
And is your code accurate? Because you are currently retrieving the same resource regardless of the param value.
Arif
3
The code is similar to this one. But I am defining the variables before using them. Open graph for images in multipage posts - #4 by jmooring. I have edited the code in my first comment.
Please confirm one way or the other.
I canβt tell if this is a reference to an asset for a particular language, or if it is something else:
(.Resources.GetMatch "thumbnail.fr*")
Arif
5
Yes. Multilingual. Instead of multipage, my layout I renamed to news. Same implementation though.
{{ $r := "" }}
{{ if and (eq .Params.layout "news") (eq .Kind "page") }}
{{ with .Parent.Resources.GetMatch "thumbnail.*" }}
{{ $r = . }}
{{ end }}
{{ else }}
{{ with .Resources.GetMatch "thumbnail.*" }}
{{ $r = . }}
{{ end }}
{{ end }}
Now that you have the Resource object in $r
, just use:
{{ $r.Permalink }}
{{ $r.Width }}
{{ $r.Height }}
{{ $r.MediaType.Type }}
or
{{ with $r }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
Note that I replaced this:
{{- with or (.Resources.GetMatch "thumbnail.*") (.Resources.GetMatch "thumbnail.fr.*") }}
with this:
{{ with .Resources.GetMatch "thumbnail.*" }}
In a multilingual site, page resources are shared unless language-specific.
1 Like
Arif
7
Yes, my resources are language specific. I saw if I do thumbnail.*
then thumbnail.fr.*
is ignored
I canβt reproduce that behavior, so maybe something specific to your site/config.
Arif
9
I tested this some time back with an older Hugo version (around Feb). So, I might have made a mistake somewhere.
system
Closed
10
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.