Can we have an option where I continue to use {{ .Content }}
and yet have an option to truncate posts at the point where I specify the <!--more-->
tag in my posts?
Using summary
truncates all posts on the homepage.
Can we have an option where I continue to use {{ .Content }}
and yet have an option to truncate posts at the point where I specify the <!--more-->
tag in my posts?
Using summary
truncates all posts on the homepage.
As written, this is false.
The .Content
of your home page is determined by what appears beneath front matter in content/_index.md
.
The .Summary
of your home page is determined by the method used to define it.
Regardless of the method used to define the .Summary
, it will only include content from content/_index.md
.
I see what you mean. Thanks for the extra information that helped me understand better.
This is what I have in my /layouts/index.html
:
{{ define "main" }}
{{ $posts := where site.RegularPages "Type" "post" }}
{{ $grouped := $posts.GroupByDate "2006-01-02" }}
{{ $paginated := (.Paginate ($grouped)) }}
<main class="content" role="main">
{{ range $paginated.PageGroups }}
{{ $thedate := (time .Key) }}
<h3 class="post-group"> {{ $thedate.Format "January 2, 2006" }}</h3>
{{ range .Pages.Reverse }}
{{ if eq .Type "post" }}
{{ .Render "article" }}
{{- end }}
{{ end }}
{{ end }}
</div>
{{ partial "pagination.html" . }}
{{ end }}
So I then head to the /layouts/post/article
, I have this:
<article class="h-entry post">
<header class="post-header">
{{ with .Params.image }}
<img class="u-featured" src="{{ . }}"></img>
{{ end }}
<h2 class="p-name post-title"><a href="{{ .URL }}">{{ .Title }}</a></h2>
</header>
<section class="e-content post-content">
<a href="{{ .Permalink }}" class="u-url read-more"><span class="dt-published" datetime="{{ .Date.Format "2006-01-02T15:04:05-0700" }}">{{ .Date.Format "03:04 PM" }} â–¸</span></a>
{{ .Content }}
{{ if .Truncated }}
<p><a href="{{ .RelPermalink }}">Read More</a></p>
{{ end }}
</section>
<footer class="post-meta">
</footer>
</article>
I added the code below after {{ .Content }}
. As you predicted, the truncate code below doesn’t work. It does work when I use {{ .Summary }}
but that also “summarizes” all posts regardless of <!--more-->
{{ if .Truncated }}
<p><a href="{{ .RelPermalink }}">Read More</a></p>
{{ end }}
The /layouts/post/summary.html
does have {{ .Content }}
as the body so I’m not sure how Summary is being determined.
Your problem statement and desired outcome are difficult to understand. Can you provide a link to the public repository for your project? It would save some time…
Also, you reference “/layouts/post/summary.html” but that isn’t called anywhere in your example.
Here you go. New to Hugo so still figuring out stuff.
When providing a repository for others to clone and build, please create a complete site (valid configuration, sample content, it builds, etc.). Thanks.
EDIT: I expect the following to work:
git clone --recurse-submodules https://github.com/am1t/musings
cd musings
hugo server
Ok. But that’s not my repository. I’m just using the theme.
The expectation is a site that we can build, not just a piece of it.
See https://discourse.gohugo.io/t/requesting-help/9132.
Let us see your code
Include a link to the source code repository of your project, because we really need the context of seeing your templates and partials to be able to help you. It is trivial to do a quick
git clone
on your repo, then runhugo server
in your project, to help you out. On the other hand, recreating your code from screenshots, or sort of guessing at it, is not.If you can’t share your repository for whatever reason, consider creating a dummy repo that you can share, which reproduces the problem you’re experiencing.
Ok. Let me restate my problem and desired outcome. I’m looking to feature full-length posts on my homepage but for some posts, I want them truncated at the point when I specify the tag.
This approach essentially disables automatic summary splitting on your site. In your site configuration:
summaryLength = 999999 # Tolstoy's War and Peace has around 600000 words.
Then in your template you can do:
{{ .Summary }}
This will return a stripped version of .Content
unless you (a) manually split the summary with the <!--more-->
tag, or (b) define the summary in front matter.
Parse the raw content for the <!--more-->
tag.
{{ if or (findRE `<!--more-->` .RawContent) .Params.Summary }}
{{ .Summary }}
{{ else }}
{{ .Content }}
{{ end }}
This will return .Content
unless you (a) manually split the summary with the <!--more-->
tag, or (b) define the summary in front matter.
Much appreciated. The second method worked!
I’ll figure out where to add the summaryLength = 999999
for the first method.
Actually, don’t use Method 1. I forgot about this issue. Tags are stripped.
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.