diff --git a/modules/wowchemy/layouts/partials/functions/get_featured_image.html b/modules/wowchemy/layouts/partials/functions/get_featured_image.html new file mode 100644 index 000000000..03a028534 --- /dev/null +++ b/modules/wowchemy/layouts/partials/functions/get_featured_image.html @@ -0,0 +1,23 @@ +{{/* Function to retrieve the featured image */}} +{{/* Inputs: page context */}} +{{/* Output: image resource, or nil if not found */}} + +{{/* + Featured image is searched in this order: + 1. Search for a file `*featured*` in the post directory + 2. Search for a file `.Params.image.filename` in the post directory + 3. Search for a file `.Params.image.filename` in the `assets/media/` directory +*/}} + +{{/* Search for an image "*featured*" in post folder */}} +{{ $resource := (.Resources.ByType "image").GetMatch "*featured*" }} +{{ if eq $resource nil }} + {{/* Otherwise fall back the image file specified in front matter */}} + {{ $filename := .Params.image.filename }} + {{/* Search in post folder */}} + {{ $resource = (.Resources.ByType "image").GetMatch $filename }} + {{/* Otherwise in `assets/media/` folder */}} + {{ if eq $resource nil }} {{ $resource = resources.GetMatch (path.Join "media" $filename) }} {{ end }} +{{ end }} + +{{ return $resource }} diff --git a/modules/wowchemy/layouts/partials/jsonld/article.html b/modules/wowchemy/layouts/partials/jsonld/article.html index e3c202af3..188a0beed 100644 --- a/modules/wowchemy/layouts/partials/jsonld/article.html +++ b/modules/wowchemy/layouts/partials/jsonld/article.html @@ -1,6 +1,6 @@ {{- $page := .page }} {{ $summary := .summary }} -{{ $featured_image := ($page.Resources.ByType "image").GetMatch "*featured*" }} +{{ $featured_image := partial "functions/get_featured_image.html" $page }} {{/* Get schema type. */}} {{ $schema := "Article" }} diff --git a/modules/wowchemy/layouts/partials/jsonld/event.html b/modules/wowchemy/layouts/partials/jsonld/event.html index a8189451a..e0add5b85 100644 --- a/modules/wowchemy/layouts/partials/jsonld/event.html +++ b/modules/wowchemy/layouts/partials/jsonld/event.html @@ -1,6 +1,6 @@ {{ $page := .page }} {{ $summary := .summary }} -{{ $featured_image := ($page.Resources.ByType "image").GetMatch "*featured*" }} +{{ $featured_image := partial "funcctions/get_featured_image.html" $page }} {{ $author := partial "functions/get_author_name" $page }}