Access content of first "post" of a taxonomy - hugo

I try to access the content of the first entry of a Taxonomy. I can get the basic fields like {{.Name}} with the following code:
<ul>
{{ range $key := .Site.Taxonomies.tags.ByCount }}
<li>
{{ with index .Pages 0 }}
{{ .Name }} <-- Name of the first post
{{end}}
{{ .Name }} ({{ .Count }})</li>
{{ end }}
</ul>
But how do I access a custom field of the first content item within the taxonomy?

The fields can be accessed by using {{ .Params }}.

Related

Hugo cannot convert type page.PagesGroup to Pages

I have a Hugo list template like this:
{{ range (.Paginate (.Data.Pages.GroupByDate "2006")).PageGroups }}
<h3>{{ .Key }}</h3>
<ul>
{{ range .Pages.ByWeight }}
<li>
{{ if .Draft }}{{ T "draft" }}: {{end}}{{ .Title | markdownify }}
<time class="date-meta">{{ .Date.Format "Jan 2" }}</time>
</li>
{{ end }}
</ul>
{{ end }}
When I run the site like this hugo server -D it works fine.
When I build the site I get:
execute of template failed: template: _default/list.html:15:14: executing "main" at <.Paginate>: error calling Paginate: cannot convert type page.PagesGroup to Pages
Turning on debug and verbose do not help. I have:
content
content/web
content/web/one.md
content/web/two.md
content/web/_index.md
content/web/three.md
content/about
content/about/index.md
What gives?
I encountered this error while writing a custom theme except it happened when running hugo -D or hugo server -D. As a workaround try wrapping it in an if condition to check .Data.Pages.
{{ if .Data.Pages }}
{{ range (.Paginate (.Data.Pages.GroupByDate "2006")).PageGroups }}
...
{{ end }}
{{ end }}

Hugo NextInSection with counter

I need to accomplish a layout with a link to next and previous post and a counter marking the actual post, bad representation ahead:
<previousPost 10/100 nextPost>
I put everything to work, except the mention of the current post number:
{{ $posts := (where .Site.RegularPages "Section" "==" "news") }}
{{ $postCount := len $posts }}
{{ $postCount }}
{{ if .PrevInSection }}
Prev Post
{{ end }}
{{ I have no idea }}/{{ $postCount }}
{{ if .NextInSection }}
Next Post
{{ end }}
But I have no clue on how to find the value of the page in the netxInSection context. I'm thinking about changing my code to a range and use the index to mark the current page but I think that should be a smarter way.
Thanks!
After some head banging on the wall I found a way to do it...
{{ range $index, $element := (where .Site.RegularPages "Type" "news" ).Reverse }}
{{ if eq . $ }}
{{- $.Scratch.Set "currentItem" (add $index 1) }}
{{ end }}
{{end}}
{{ $posts := (where .Site.RegularPages "Section" "==" "news") }}
{{ $postCount := len $posts }}
{{ if .PrevInSection }}
prev
{{ else }}
prev
{{ end }}
<div class="page-navigation__counter">
{{ $.Scratch.Get "currentItem" }}/{{ $postCount }}
</div>
{{ if .NextInSection }}
next
{{ else }}
next
{{ end }}

Hugo - Access .Pages from the context of the list template from within the context of the single template

The documentation for hugo says the the .Pages variable inside the context of the single page is blank and that the .Pages variable from the context of the list page is populated.
I want to access the .Pages variable from the list page INSIDE the context of the single page.
How do I do this?
Documentation is below:
Worked through the issue here is what I came up with. This snippet:
{{ $currentPage := . }}
{{ range .Site.Pages }}
{{ if .InSection $currentPage}}
{{ if .IsAncestor $currentPage }}
{{ else }}
<li>
<a class="nav-link active" href="{{.Permalink}}">{{.Title}}</a>
</li>
{{ end }}
{{ end }}
{{ end }}

How does variable scope work in Hugo for different pages?

How does variable scope work in Hugo for for different pages?
I can make a list of post titles on my home page by adding this code to themes\[theme name]/layouts/_default/list.html:
<ul>
{{ range .Data.Pages.ByPublishDate }}
<li>
{{ .Title }}
</li>
{{ end }}
</ul>
However the same code in a standalone page content/archive.md produces nothing. How do I get a standalone page to list entries from the /post folder?
the .Data object is scoped to the content type it is called within. To access different type of content use the .Site object on which you can access the .Pages object. That object contains all the pages of all content types. Simply filter for the content type you want using the where function, where .Site.Pages "Type" "post".
So your code becomes:
<ul>
{{ range (where .Site.Pages.ByPublishDate "Type" "post") }}
<li>
{{ .Title }}
</li>
{{ end }}
</ul>

How to List Pages in Current Section

I'm trying to list the pages in the current url section.
Get All Sections
Limit range to current Section
List pages in current Section
{{ range $value := .Site.Sections }}
{{ range .Section }}
{{ range $value.Pages }}
<ul>
<li>{{ .Title }}</li>
</ul>
{{ end }}
{{ end }}
{{ end }}
Though it returns null because {{ range .Section }} is not valid code.
What is the correct way to do this?
https://gohugo.io/templates/variables/
You need to filter .Site.Pages by section using the where function. Try this:
<ul>
{{ range where .Site.Pages "Section" .Section }}
<li>{{ .Title }}</li>
{{ end }}
</ul>
If you want to avoid empty lists, you can store the slice of section pages in a variable and check its length before you output the ul tags.
{{ $sectionPages := where .Site.Pages "Section" .Section }}
{{ if ge (len $sectionPages) 1 }}
<ul>
{{ range $sectionPages }}
<li>{{ .Title }}</li>
{{ end }}
</ul>
{{ end }}

Resources