diff --git a/layouts/partials/shortcodes/attachments.html b/layouts/partials/shortcodes/attachments.html
index 49ac7c7dd5..b9485429e1 100644
--- a/layouts/partials/shortcodes/attachments.html
+++ b/layouts/partials/shortcodes/attachments.html
@@ -32,10 +32,18 @@
{{ if $icon }}{{ end }}{{ if and $icon $title }} {{ end }}{{ $title | markdownify }}
- {{- $fileLink := printf "%s/%s" (.Language.ContentDir | default "content") .File.Dir }}
- {{- $fileLink = replace (replace $fileLink "\\" "/") "content/" "" }}
- {{- $fileDir := printf "%s/%s" (.Language.ContentDir | default "content") .File.Dir }}
- {{- $fileDir = replace $fileDir "\\" "/" }}
+ {{- $version := split hugo.Version "." }}
+ {{- $major := int (index $version 0) }}
+ {{- $minor := int (index $version 1) }}
+ {{- $fileDir := "" }}
+ {{- if or (and (eq $major 0) (ge $minor 112)) (gt $major 0) }}
+ {{- $fileDir = path.Dir (strings.TrimPrefix (path.Clean hugo.WorkingDir) (path.Clean .File.Filename)) }}
+ {{- else }}
+ {{- $fileDir = printf "%s/%s" (path.Clean (.Language.ContentDir | default "content")) (path.Clean .File.Dir) }}
+ {{- end }}
+ {{- $fileDir = printf "%s/" (path.Clean (strings.TrimPrefix "/" $fileDir)) }}
+ {{- $fileLink := $fileDir }}
+ {{- $fileLink = replace $fileLink "/content/" "" }}
{{- $filesName := printf "%s.files" .File.BaseFileName }}
{{- if and (eq .File.BaseFileName "index") (fileExists (printf "%sfiles" $fileDir)) }}
{{- $c := "" }}{{/* backward compat to < 5.9.0 behavior */}}