mirror of
https://github.com/Brandon-Rozek/website.git
synced 2025-03-23 21:11:18 +00:00
New post
This commit is contained in:
parent
77f7dd1bec
commit
a48c043a82
1 changed files with 51 additions and 0 deletions
51
content/blog/hugo-hiding-section-from-listing.md
Normal file
51
content/blog/hugo-hiding-section-from-listing.md
Normal file
|
@ -0,0 +1,51 @@
|
|||
---
|
||||
title: "Hiding Section From Listing in Hugo"
|
||||
date: 2022-05-19T22:43:04-04:00
|
||||
draft: false
|
||||
tags: []
|
||||
math: false
|
||||
---
|
||||
|
||||
In Hugo you can list all the sections using the following code
|
||||
|
||||
```html
|
||||
{{ range .Site.Sections }}
|
||||
<li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
However what if there's a section that you want to hide for whatever reason? Let us say that you have a section labeled "unlisted" that you want to hide. The directory structure can look like this:
|
||||
|
||||
```
|
||||
content/
|
||||
posts/
|
||||
unlisted/
|
||||
bookmarks/
|
||||
// ...
|
||||
```
|
||||
|
||||
There are two types of hidden that I can think of:
|
||||
|
||||
- You don't want any pages within unlisted to render at all.
|
||||
- You want it to render, but not appear in the section listing
|
||||
|
||||
For the first case, [Filosophy suggests](https://filosophy.org/code/disabling-a-specific-section-in-hugo/) to rename the section so that it starts with a dot. For example, `.unlisted`.
|
||||
|
||||
For the second case, we need to introduce a page variable to help us choose when to display it. Let us call that page variable `hidden`. To set it to true, you need to add it to the frontmatter of `content/unlisted/_index.md`.
|
||||
|
||||
```yaml
|
||||
---
|
||||
hidden: true
|
||||
---
|
||||
```
|
||||
|
||||
Then replace the listing code with the following:
|
||||
|
||||
```html
|
||||
{{ range .Site.Sections }}
|
||||
{{ if not .Params.hidden }}
|
||||
<li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
```
|
||||
|
Loading…
Reference in a new issue