如何更改 hugo 中的主页?

How to change the homepage in hugo?

提问人:jjk 提问时间:5/5/2019 更新时间:3/14/2023 访问量:9041

问:

我怎样才能有 /posts 作为主页?

我应该重定向,更改 hugo 1配置中的 baseURL 还是在 主题 2配置?

脚注

1 https://gohugo.io/getting-started/configuration/

2 https://github.com/luizdepra/hugo-coder/wiki/Configurations

雨 果

评论


答:

2赞 Saif Azmi 5/10/2019 #1

您可以修改 home.html 文件,因为 index.html 文件正在嵌入它,而 index 中没有其他内容.html

https://github.com/luizdepra/hugo-coder/blob/master/layouts/partials/home.html

在上述文件中进行更改 theme/layouts/partials/home.html 这些更改将在您保存文件后立即在网站上生效(如果您已经在运行$ hugo server -D)

评论

0赞 jjk 6/3/2019
谢谢@Saif阿兹米。您知道我如何确定 /posts 基础中的索引在哪个文件上吗?因此,我可以按照您的建议将其内容复制到 home.html。在此处修改当前文件不会导致 /posts 索引的更改:github.com/luizdepra/hugo-coder/tree/master/layouts/partials/...
0赞 Saif Azmi 6/3/2019
/posts 的索引由文件呈现。可以在这里找到:github.com/luizdepra/hugo-coder/blob/master/layouts/posts/......list.html
1赞 jjk 6/5/2019
谢谢@Saif阿兹米。将 home.html 的内容替换为 list.html 中的内容后,主页变为空。还有其他想法吗?也许我应该强调我的目标:我希望访问者登陆 /posts 而不是主页。类似于wordpress的“使用另一个页面作为主页”功能。
0赞 Saif Azmi 6/28/2019
然后只需将索引页中的代码替换为帖子中的代码即可。
2赞 Yurii Rochniak 1/23/2021 #2

对我来说,它有助于将文件添加到我的主题中。以下是其内容:layouts/index.html

{{ define "main" }}
  {{ $pag := .Paginate (where site.RegularPages "Type" "in" site.Params.mainSections ) 6 }}
  <div class="archive-body">
    {{ range $pag.Pages }}
      {{ .Render "li" }}
    {{ end }}
  </div>

  {{ partial "pagination" . }}
{{ end }}

"li"是一个部分 HTML 模板,它为我呈现了一个页面。

然后我不得不在我的 .由于我的内容位于目录内,因此这是配置。mainSectionsconfig.tomlcontent/post

[params]
  mainSections = ["post"]

由于这是一个列表,因此您应该能够添加多个部分。例如,如果你的内容是传播的,比如说在和之间,等等。不过,我还没有尝试过。content/postcontent/articles

1赞 kozone 12/15/2022 #3

我知道这是一个老问题,但对我来说,将特定的 Markdown 页面设置为登录页面的最简单方法是简单地创建一个来覆盖我的主题,并将其放入其中:

这样,我可以保留所有主题的样式,而不必担心编辑模板,而只专注于创建内容。作为 hugo 的新手,这作为鹈鹕的替代品效果很好。
layouts/index.html<script>window.location = "/mainlist"</script>save_as: index.html

0赞 nyukeit 3/14/2023 #4

正如 Saif 所提到的,您需要替换位于“partials”文件夹中的文件中的内容。home.html

# Remove 
{{ partialCached "home/avatar.html" . }}
{{ partialCached "home/author.html" . }}
{{ partialCached "home/socials.html" . }}

# Add
{{ partialCached "posts/li.html" . }}

这应该在主页上列出您的博客。但是,博客列表也带有自己的标题。这意味着您将拥有两个标题。要从博客列表中删除标题,请从 .index.html

# Remove these lines from the file
    <header>
      <h1 class="title">
        <a class="title-link" href="{{ .Permalink | safeURL }}">
          {{ title (i18n (lower .Title)) | default .Title }}
        </a>
      </h1>
    </header>

现在您将拥有一个带有博客列表且没有 2 个标题的主页。

确保对您编辑的文件进行备份。