vodoraslo.xyz/public/blog/multiple-index-pages-in-hugo/index.html
2023-04-29 13:36:49 +03:00

148 lines
6.2 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<title>Multiple Index Pages in Hugo | vodoraslo</title>
<link rel="canonical" href="https://vodoraslo.xyz/">
<link rel='alternate' type='application/rss+xml' title="vodoraslo RSS" href='/index.xml'>
<link rel='stylesheet' type='text/css' href='/style.css'>
<link rel="icon" href="/favicon.ico">
<meta name="description" content="This is how to create multiple index pages in Hugo I wanted to order Hackbook in reverse (i.e. oldest to newest) so that it&rsquo;s easier for the reader to start at the correct page.
I ran into the following problem - the default list.html does them from newest to oldest.
So I found this forum post and I created a file in the _default directory as follows:
layouts
|----**_default**
|-------**hackbook**
|-----------**order-by-oldest."/>
<meta name="keywords" content="blog">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="robots" content="index, follow">
<meta charset="utf-8">
</head>
<body>
<main>
<header><h1 id="tag_Multiple Index Pages in Hugo">Multiple Index Pages in Hugo</h1></header>
<article>
Table Of Contents:<nav id="TableOfContents">
<ul>
<li><a href="#this-is-how-to-create-multiple-index-pages-in-hugo">This is how to create multiple index pages in Hugo</a></li>
<li><a href="#using-your-custom-_indexhtml">Using your custom _index.html</a></li>
</ul>
</nav>
<div class="post-content"><h2 id="this-is-how-to-create-multiple-index-pages-in-hugo">This is how to create multiple index pages in Hugo<a hidden class="anchor" aria-hidden="true" href="#this-is-how-to-create-multiple-index-pages-in-hugo">#</a></h2>
<p>I wanted to order <a href="/library/hackbook">Hackbook</a> in reverse (i.e. oldest to newest) so that it&rsquo;s easier for the reader to start at the correct page.</p>
<p>I ran into the following problem - the default <code>list.html</code> does them from newest to oldest.</p>
<p>So I found <a href="https://discourse.gohugo.io/t/two-home-pages/31312/9">this forum post</a> and I created a file in the <code>_default</code> directory as follows:</p>
<pre tabindex="0"><code>layouts
|----**_default**
|-------**hackbook**
|-----------**order-by-oldest.html**
|-------baseof.html
|-------index.html
|-------list.html
|-------single.html
</code></pre>
<figure ><img src="/img/custom-index-directory.PNG" alt="Picture of the directory"></figure>
<p>I named my file order by oldest because I plan on reusing it in other places. This is what&rsquo;s contained inside it:</p>
<pre tabindex="0"><code>{{ define &#34;title&#34; -}}
{{ .Title | title }}
{{- end }}
{{ define &#34;main&#34; -}}
{{ .Content }}
&lt;ul&gt;
{{- range.Pages.Reverse }}
&lt;li&gt;
{{- if .Param &#34;datesinlist&#34; }}&lt;time datetime=&#34;{{ .Date.Format &#34;2006-01-02T15:04:05Z07:00&#34; }}&#34;&gt;{{ .Date.Format &#34;2006 Jan 02&#34; }}&lt;/time&gt; &amp;ndash; {{ end -}}
&lt;a href=&#34;{{ .RelPermalink }}&#34;&gt;{{ .Title }}&lt;/a&gt;
{{- if .Param &#34;authorsinlist&#34; }}
{{- range .Param &#34;authors&#34; }} by {{ . }}{{ end -}}
{{ end -}}
&lt;/li&gt;
{{- end }}
&lt;/ul&gt;
{{- end }}
</code></pre><p>If you want to display the date on the left of the titles, you have to add <code>datesinlist=true</code> in your config.toml or <code>datesinlist: true</code> in your config.yaml</p>
<p>You probably don&rsquo;t need <code>enableGitInfo = true</code> as that will crash your website, I have no idea what it does, you don&rsquo;t need it.</p>
<h2 id="using-your-custom-_indexhtml">Using your custom _index.html<a hidden class="anchor" aria-hidden="true" href="#using-your-custom-_indexhtml">#</a></h2>
<p>After creating your custom _index.html you&rsquo;d use it as follows:</p>
<ol>
<li>Create an _index.md file in your desired directory</li>
<li>Add <code>layout: &quot;hackbook/order-by-oldest&quot;</code> to your preamble (if you named your folder and or file something else, you have to change it here)</li>
</ol>
<figure ><img src="/img/layout-custom-index.PNG" alt="Picture of the layout in the preamble"></figure>
<p>And now you should have a custom _index.html for your pages! :)</p>
<p>Here is a verbose copy paste of the original hugo forum answer in case it gets deleted:</p>
<pre tabindex="0"><code>Content structure:
content
├── better
│ └── _index.md
├── post
│ ├── post-1.md
│ ├── post-2.md
│ └── post-3.md
└── _index.md
content/better/_index.md
+++
title = &#34;Better&#34;
date = 2021-03-04T17:02:42-08:00
draft = false
type = &#34;post&#34;
layout = &#34;posts-by-lastmod&#34;
+++
Template structure:
layouts
├── _default
│ ├── baseof.html
│ ├── list.html
│ └── single.html
├── post
│ └── posts-by-lastmod.html
└── index.html
layouts/post/posts-by-lastmod.html
{{ define &#34;main&#34; }}
{{ .Content }}
{{ range (where .Site.RegularPages &#34;Type&#34; &#34;post&#34;).ByLastmod.Reverse }}
&lt;h2&gt;&lt;a href=&#34;{{ .RelPermalink }}&#34;&gt;{{ .Title }}&lt;/a&gt;&lt;/h2&gt;
{{ end }}
{{ end }}
layouts/index.html
{{ define &#34;main&#34; }}
{{ .Content }}
{{ range (where .Site.RegularPages &#34;Type&#34; &#34;post&#34;).ByDate.Reverse }}
&lt;h2&gt;&lt;a href=&#34;{{ .RelPermalink }}&#34;&gt;{{ .Title }}&lt;/a&gt;&lt;/h2&gt;
{{ end }}
{{ end }}
config.toml (see https://gohugo.io/variables/git/#lastmod)
enableGitInfo = true
</code></pre>
</div>
<div id="nextprev">
<a href="/updates/yoinked-css/"><div id="prevart">Previous:<br>Yoinked some Css and updated the site</div></a>
<a href="/blog/meta-description-in-hugo/"><div id="nextart">Next:<br>Meta Description in Hugo</div></a>
</div>
<div style="padding-top: 1.5em">
<div style="clear:both" class=taglist>
Tags: [<a id="tag_blog" href="https://vodoraslo.xyz/tags/blog">Blog</a>]
</div>
</div>
</article>
</main>
<footer class="rssSvg">
<div style="padding-bottom: 0.5em;"><a href="https://vodoraslo.xyz/">Homepage</a></div> <div><a href="/index.xml"><img src="/rss.svg" style="max-height:1.5em" alt="RSS Feed" title="Subscribe via RSS for updates."></a></div>
</footer>
</body>
</html>