diff --git a/layouts/shortcodes/adsense.html b/layouts/shortcodes/adsense.html new file mode 100644 index 0000000..886890b --- /dev/null +++ b/layouts/shortcodes/adsense.html @@ -0,0 +1,38 @@ +{{- /* 只在生产环境中渲染广告 */ -}} +{{- if hugo.IsProduction -}} + + {{- /* 获取传入的第一个参数,例如 "in-article" */ -}} + {{- $adType := .Get 0 -}} + + {{- /* 在这里硬编码你的发布商ID */ -}} + {{- $client := "ca-pub-xxxxxxxxxxxxxxxx" -}} {{/* <--- 替换成你的发布商ID */}} + + {{- /* 声明一个变量来存储广告位ID */ -}} + {{- $slot := "" -}} + + {{- /* 根据传入的参数,选择对应的广告位ID */ -}} + {{- if eq $adType "in-article" -}} + {{- $slot = "8277576073" -}} {{/* <--- 替换成你的文章内广告位ID */}} + {{- else if eq $adType "sidebar-square" -}} + {{- $slot = "8961453426" -}} {{/* <--- 替换成你的方形广告位ID */}} + {{- else if eq $adType "another-type" -}} + {{- $slot = "8697428225" -}} {{/* <--- 你可以继续添加更多类型 */}} + {{- end -}} + + + {{- /* 如果找到了对应的广告位ID,则渲染广告代码 */ -}} + {{- if $slot -}} +