Listing all posts

I have started off this year with moving to a new theme, which is in every way excellent, but which also lacks some of the functionality of the previous theme. (I switched because the old theme didn’t seem to be updated.)

I have read a number of posts here about this topic, and spent some time looking at documentation, but I still can’t get it t work. I simply want a page, called say “All posts” which lists all posts by year. This is something the previous theme provided and the theme I have moved to doesn’t. And it’s not simply a matter of copying files from the old to the new theme, as their structures are different.

Ideally, I’d have a directory /all_posts/ containing just one file index.md which I could reference with my config file, and that index.md file would simply contain the necessary scripts or shortcodes for creating a list of all blog posts. (This is just to keep it simple, and to avoid messing up the theme.)

I’ve been playing around with some of the material on Lists of Content in Hugo | Hugo but I can’t seem to get them to work - clearly I’m missing out on something.

As I say, I don’t want to have to fiddle with new files in the theme directory; I’d rather do that under my home directory - if possible. Many thanks!

(I also appreciate that this is a question that has been asked and answered many times, but my reading so far has not helped me. In my time of using Hugo I have always concentrated on blog content rather than structure, and I’ve let the theme take care of that. But now I need to do a little structural fiddling, which is leading me into areas of which I have no knowledge and less experience!)

You can try the following:

  1. Create a file content/all-posts.md.
  2. Add layout: all-posts to its frontmatter.
  3. Create a file layouts/page/all-posts.html.
  4. Add the following content to it:
{{- range .Data.Pages.GroupByDate "2006" -}}
  {{- range .Pages -}}
    <h3>
      <a href = "{{- .RelPermalink -}}">
        {{- .Title -}}
      </a>
    </h3>
  {{- end -}}
{{- end -}}

I haven’t tested this myself, but it might work with additional debugging. Also, the structure might change a bit depending on your theme’s configuration.

1 Like

use site.Pages …

I have a page in my site at /archives using this template in layouts/_default/archives.html

This lists all posts in one page, grouped by month and year. I have a “mainSections” param in my config.toml file that lets me pick which content types should be listed here, or displayed on the front page of the site.

mainSections = ["posts", "podcast"] # list any other content types to include

It’s viewable at Archives - D'Arcy Norman dot net

{{ define "main" }}

<div class="container" role="main">
  <div class="row">
    <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
  <main>
      {{ .Content }}

	{{ $pages := where site.RegularPages "Type" "in" site.Params.mainSections }}
	{{ range $pages.GroupByPublishDate "2006-01" }}
	<h2 class="archive-year" id="{{.Key | urlize}}">{{.Key}}</h2>
		<ul class="year-of-posts">
			{{ range sort .Pages "PublishDate" "asc" }}
			<li>
				<p>
					<time datetime="{{ .PublishDate.Format "2006-01-02T15:04:05-07:00" }}"></time>
					{{ .PublishDate.Format "January 2" }}: <a href="{{.Permalink}}">{{.Title}} </a>
				</p>
			</li>
			{{end}}
		</ul>
	{{end}}
  </main>
</div>
</div>
</div>

{{end}}