返回
Featured image of post 最后更新时间 Hugo

最后更新时间 Hugo

在文章头部与主页显示最后更新时间

最后修改时间:2024 年 11 月 01 日 01 时 59 分 24 秒

距今已经 0 天 0 小时 0 分 1 秒没有修改

本文主要是 如何添加 文章最后修改时间 ,

位置: 文章标题 头部 与 文章创建时间 在同一行

修改后的样式

image-20240926190240008

image-20240926190326120

具体步骤

前提条件:

  1. hugo博客
  2. stack主题

任何修改前建议做好备份工作

第一步: 在config.yaml或者(hugo.yaml)添加如下配置:

文件内任意位置,建议在最底部

1
2
3
4
5
6
7
8
9
# 文章底部的最后更新时间
# frontmatter:
#     lastmod: :fileModTime

frontmatter:
  lastmod: ["lastmod", ":fileModTime", ":defalut"]    
  
# enableGitInfo: true
# gitRepo: "https://github.com/iwyang/hugo"
  • :fileModTime:文件修改时间
  • lastmod:文章里 lastmod 字段
  • :defalut:默认时间

第二步: details.html文件内修改

位置: dev\layouts\partials\article\components\details.html

(如果没有建议从themes文件夹内的相同位置复制到上面的位置)

插入代码的具体行数有要求:

1
2
3
4
5
6
7
8
		
		{{ if ne (.Lastmod.Format "2006-01-02 15:04:05") (.Date.Format "2006-01-02 15:04:05") }}
			<div>
				{{ partial "helper/icon" "updateTime" }}
				<p> {{ .Lastmod.Year }} 年 {{ printf "%02d" .Lastmod.Month }} 月 {{ printf "%02d" .Lastmod.Day }} 日 {{ printf "%02d" .Lastmod.Hour }} 时 {{ printf "%02d" .Lastmod.Minute }} 分 {{ printf "%02d" .Lastmod.Second }} 秒</p>
			</div>
		{{ end }}
		

注意代码具体行数 在 文章创建日期代码后面

image-20240926185119098

第三步: 图标样式下载

图标下载网址: 免费图标SVG,PNG,ICO或ICNS (icon-icons.com)

涉及到的代码

1
{{ partial "helper/icon" "updateTime" }}

updateTime 名字改为 你的图标名字

建议 英文名字

图标位置: dev\assets\icons\updateTime.svg

注意事项:

  1. 图标为英文名
  2. 后缀为 svg

总结

涉及三个文件的修改

参考博客

  1. Hugo Stack 主题配置与使用 | Bore’s Notes
  2. 自动添加博客页面更新日期 | (yfei.page)

末尾行

Licensed under CC BY-NC-SA 4.0
最后更新于 Nov 01, 2024 01:59 UTC
本博客已稳定运行
发表了20篇文章 · 总计19.98k字
本博客已稳定运行
发表了20篇文章 · 总计19.98k字
//--------------------------------------------------