1

I have a site using the hugo-coder theme, which has a layouts/posts folder that specifies that anything in the "posts" folder will have a blog post format.

I would like to have two different blogs in two different subdirectories, using the same layout. Is there a way to tell Hugo that the content/blog1 directory should use the same settings and layout as the content/posts directory without copying themes/hugo-coder/layouts/posts into layouts/blog1? Ideally I would avoid using symlinks, because, while convenient, I've had a decent amount of software throw weird errors when I use symlinks, so I avoid them when it's possible.

Paul
  • 10,381
  • 13
  • 48
  • 86

2 Answers2

1

You can set the layout or type field to posts in the frontmatter of your _index.md file in content/blog1.

See this docs page for more info.

Edit: Alternatively, you could create an archetype for blog1 that automatically sets the value to posts in the frontmatter of individual posts in that section, assuming you're using hugo new blog1/postname.md to create posts for that section.

Double edit: The first suggestion didn't work. You could also create subsections within content/posts/blog1 and set the permalinks of posts in that subsection to use the last section only. That should remove the need to explicitly set the type in post frontmatter every time because each post would already have a type of posts.

In config.toml:

[permalinks]
    posts = "/:sections[last]/:slug/"
wjh18
  • 685
  • 1
  • 6
  • 13
  • Am I doing it wrong? When I do `layout = "posts"` or `type = "posts"`, it sets the index page *itself* to be a blog post, and all the sub-pages are still pages of type "myblog1". I'd rather not have to explicitly set the layout on each page in "myblog1", even if it is set by a template. – Paul Feb 28 '22 at 19:53
  • Hmm, I could've sworn that was possible..maybe not. You could also create subsections within `content/posts/blog1` and set the permalinks of posts in that subsection to use the last section only. This is what I do in my blog. That should remove the need to explicitly set the type in post frontmatter every time because each post would already have a type of posts. The only downside to this (that I haven't found a suitable solution for) is that list pages only will still contain `posts` in the permalink. Only singles will omit it. I'll update the post with the permalink config. – wjh18 Feb 28 '22 at 20:22
  • This works for me. @Paul, Have you created a new archetype for "blog" or whatever you're calling it? That may be the missing piece here. Then setting the "type: blog" in the content frontmatter should apply the template from layouts/blog/single.html. – Brian Wagner Mar 01 '22 at 23:20
0

You can use a partial in your templates. If you do that you WILL need the single and list file in the layouts/blog directory, but it could be an empty file referencing the partial. The layouts/posts/single.html and the layouts/blog/single.html both will then look like this:

{{ partial "singleblog.html" . }}

Compeletely DRY... and without much complexity.

Mr. Hugo
  • 11,887
  • 3
  • 42
  • 60