Find out if current page is single post

Is there any way to find out, if I am in a single post page at the moment of rendering the template? I know about .IsPage which is true if the page is a simple page. But I want to add code to a single post (in the header) and not in list pages and “normal” pages and the home page.

You would need to add the single page layout stuff to single.html.


What is the difference between single pages and “normal” pages. You might need to give some examples.

A normal page would be ie. the contact page… and the other point were “LIST” pages, not single :wink:

the specific user case would be the og:type tag for open graph info:

  • blog post would be “article”
  • all other pages would be “website”

I sniffed around in other themes and they all seem to solve this via front matter. which is a hassle for me because I imported 2000 posts from wordpress and would have to add that manually.

I think it might be solvable with a custom block {{ block "type" . }}{{ end }} and then in a single post layout file the accompanying {{ define "main" }}article{{end}} and everywhere else {{ define "main" }}website{{end}}.

To Hugo, a contacts page (contents/ or contents/contact/ would be the same as a post page (contents/posts/ or contents/posts/some-post/… both will get rendered using single.html (unless you customize the type and/or layout in the front-matter).

I didn’t follow that.

The total number is less relevant… the number of posts where you need to refactor is important.

Say you have 1950 posts and 50 non-posts… then you need to add front-matter only to the 50 non-posts. You can then use with in the layout to distinguish between those 2 (or more sets) of regular pages.

yeah, well… I didn’t even think that far :slight_smile: that’s the best solution.

Well you could divide your content into content/post/ and content/page/ and drop your “normal” pages in the latter.
From now you won’t have to specify the type in Front Matter, all of your posts will be of Type “post”, all of your “normal” pages will be of Type “page”. (not to confuse with Kind :slight_smile: )

Then rewrite the page url with config.toml:

  page = "/:slug/"

I am not even sure you have to modify permalinks…

1 Like