From 24be2a5ed1a3fa139ad152703d5bdb7fedcec693 Mon Sep 17 00:00:00 2001 From: Holger Huo <50446405+HolgerHuo@users.noreply.github.com> Date: Wed, 22 May 2024 11:18:42 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Add=20breadcrumb=20to=20schema?= =?UTF-8?q?=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- layouts/partials/schema.html | 45 +++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/layouts/partials/schema.html b/layouts/partials/schema.html index 1681f5f..6eb53b1 100644 --- a/layouts/partials/schema.html +++ b/layouts/partials/schema.html @@ -1,3 +1,17 @@ +{{- define "partials/inline/breadcrumbData" -}} + {{- $scratchCtx := .scratch -}} + {{- with .parent }} + {{ partial "inline/breadcrumbData" (dict "scratch" $scratchCtx "parent" .Parent) }} + {{- $scratchCtx.Add "listItem" (slice (dict + "@type" "ListItem" + "position" ($scratchCtx.Get "count") + "name" (.Title | humanize | title) + "item" .Permalink + )) -}} + {{- $scratchCtx.Add "count" 1 -}} + {{- end }} +{{- end -}} + {{ if .IsHome -}} -{{ end }} + {{- /* Source: https://bullaki.com/projects/web-design/seo-with-hugo-5-breadcrumbs/ */}} + {{- $scratch := newScratch -}} + {{- $scratch.Set "count" 1 -}} + {{ partial "inline/breadcrumbData" (dict "scratch" $scratch "parent" .Parent) }} + + {{- with index (.GetTerms "categories") 0 -}} + {{- $scratch.Add "listItem" (slice (dict + "@type" "ListItem" + "position" ($scratch.Get "count") + "name" .LinkTitle + "item" .Permalink + )) -}} + {{- $scratch.Add "count" 1 -}} + {{- end }} + + + {{- $scratch.Add "listItem" (slice (dict + "@type" "ListItem" + "position" ($scratch.Get "count") + "name" (.Title | humanize | title) + )) -}} + + {{- $scratch.SetInMap "breadcrumb" "@context" "https://schema.org" -}} + {{- $scratch.SetInMap "breadcrumb" "@type" "BreadcrumbList" -}} + {{- $scratch.SetInMap "breadcrumb" "itemListElement" ($scratch.Get "listItem") -}} + + +{{- end }}