Pagination and group by date


#1

I’m trying to sort through two things at once: pagination, and GroupByDate. I imported a bazillion posts from an old weblog and am trying to reconstruct a weekly or monthly view of archives.

The snippet I have now reads

{{ range .Data.Pages.GroupByDate "2 January 2006"  }}
<h2>{{ .Key }}</h2>
 {{ range .Pages }}
      <h3><a href="{{ .Permalink }}">{{ .Title }}</a></h3>
      {{ .Content }}
  {{ end }}
{{ end }}

which nicely groups by date. However with a bazillion posts the front page gets too big, so I need to paginate.


Pagination by content date
#2

The built-in paginator in Hugo doesn’t currently support PagesGroup (the return value from .Data.Pages.GroupByDate). It shouldn’t be hard to support that, but someone would have to do some work (please open a issue at GitHub).

It should, however, be possible to use the current paginator with .Data.Pages and similar, and do the grouping “by hand”.


#3

This reply is very related, but if you would like me to start a new thread, I can.

I’m trying to create a view so that when someone navigates to http://localhost:1313/2015 they can see all posts in that year, and same with http://localhost:1313/2015/01/ they can see all posts made in Jan 2015.

Note: http://localhost:1313/2015/01/test-post/ does render the proper post, but there are no “index.html” files in the 2015 or 01 folders.

I’ve read and reread the following pages on gohugo.io:

  • /templates/list/
  • /content/sections/,

and https://github.com/spf13/hugo/tree/master/examples/blog.

It seems as though I need to create something like this, but it doesn’t work.

/layouts/2015/2015.html
/layouts/2015/01/01.html

from the example blog, they have /layouts/indexes/post.html which I don’t understand entirely, nor does it really do what I’m trying to do. What’s special about the indexes folder name, I can’t find that in the documentation.


#4

@Weston_McNamee this thread is related: http://discuss.gohugo.io/t/creating-list-view-for-custom-permalink-solved/1352/2


#5

now that I know that this isn’t really supported (I read your link chain), I came up with a nifty solution using taxonomies.

#/config.yaml
...
taxonomies:
  2015: "2015"
...
#/layouts/taxonomy/2015.html
<h1> 2015 month (layouts\taxonomy\2015.html) </h1>

{{ printf "%#v" . }}
#/layouts/_default/terms.html
<h1> terms default </h1>

{{ printf "%#v" . }}
#/content/post/test-post.md (yaml)
date: "2015-02-14T21:15:34-07:00"

2015:
  - "02"

With the following in place, I successful get an index.html file in at urls /2015/ and /2015/02/, and with the debug print, it will allow me to continue development and refine these pages.

so, config.yaml is fairly easy to implement, 1 taxonomy per year, the post generation (to include the year/mo taxonomy) could probably done with archetypes?


#6

Creative


#7

here’s how to get a list of archives & links by year/month

<ul>
    {{ range $value := .Site.Pages.GroupByDate "January 2006" }}
        {{ $url := (index $value.Pages 0).Node.Date.Format "2006/01" | urlize }}
        <li><a href="/{{ $url }}">{{ .Key }}</a></li>
    {{ end }}
</ul>

html output:

<ul>                                    
    <li><a href="http://localhost:1313/2015/12">December 2015</a></li>   
    <li><a href="http://localhost:1313/2015/08">August 2015</a></li>  
    <li><a href="http://localhost:1313/2015/06">June 2015</a></li>  
    <li><a href="http://localhost:1313/2015/02">February 2015</a></li> 
    <li><a href="http://localhost:1313/2014/03">March 2014</a></li>
    <li><a href="http://localhost:1313/2014/01">January 2014</a></li>
</ul>

#8

@Weston_McNamee Thanks for this solution. Adding the taxonomies might work and it’s not a lot of but I would prefer native support from Hugo. Sounds like a feature request, or not?


#9

I think it would be useful to learn Go. When I do, I’ll be sure to submit a pull request. Until then, let the hacking continue. :slight_smile:


#10

This topic has drifted far, far away from the topic starter … Lets get it back on track:


#11

I came up with an alternative solution that solves the permalink problem (creating index.html for /2015, /2015/08, etc) using sections rather than taxonomies. See this post on my blog for details.