Content: is it same for Page and Post? And how to hide date

Hello!

I’m about confused.

When we create content via huge new something.md it creates content with
date = "2015-12-09T15:03:01+09:00" and shows it up, but I may not want to show the date.
This would be ok if I’m making a blog post.

Do we create a page also via huge new something.md ? If yes, is there a way to hide date?
Removing the filed, still shows up but with the default date.

Thanks.

UPDATE:

It seems hiding/showing date is set by the
<span class="post-date">{{ .Date.Format "Mon, Jan 2, 2006" }}</span>
so removing this line can hide a date.

Please correct me if I’m wrong.

You’re correct. Every page has a date, even if you don’t set it explicitly. The layouts and/or theme controls whether it is displayed or not.

1 Like

@askar To expand a little on @moorereason 's response:

I’m assuming that <span class="post-date">{{ .Date.Format "Mon, Jan 2, 2006" }}</span> is from a Hugo theme that you’ve downloaded. Although you may not want to have the date write to the page for your end-user, you’ll eventually be grateful that this little chunk of metadata is included in all of your content files.

You mentioned that writing the date to the page “would be okay if making a blog post,” so I’m assuming you’ll need it for some of your content. If you want to set this at the page/layout level, you can add page/content-specific metadata in the toml/yaml head of your new .md file and then create a conditional statement for displaying the date.

For yaml:

---
title: "My Blog Post"
date: "2015-12-09T15:03:01+09:00"
showthedate: true
---

For TOML:

title = "My Blog Post"
date = "2015-12-09T15:03:01+09:00"
showthedate = true

You can make this easier by deciding if you want a default value for particular sections. So, for example, you may want all posts to show the date (you can also use .PublishDate, which is a built-in page variable with Hugo) by default and create a file in your archetypes folder named post.md that shows that looks like the following:

---
showthedate: true
---

Which will create a file that includes both the title and date by default and then will add your new param each time you create a new post from the command line.

Now you can access this little chunk of metadata using Go templating logic as follows:

{{ if .Params.showthedate }}
<span class="post-date">{{ .Date.Format "Mon, Jan 2, 2006" }}</span>
{{ end }}

If you want to give yourself the opportunity to turn off all dates site-wide by changing a single variable but also leave yourself the granular control on a per-page basis, you could create a site variable in your config file (toml or yaml). You wouldn’t want to do this for dates, but you may have other use cases (eg, wanting to turn off all comments at once for something like Disqus) and it will help give you an idea of the difference between site variables and page variables:

####TOML

#This would be in your config.toml file at your root
[params]
    ShowDatesOnPosts = true

####YAML

#This would bet in your config.yaml file at your root

params:
- ShowDatesOnPosts: true

You could then set this in your templating logic to look for both conditions:

{{ if and (.Params.showthedate) (.Site.Params.ShowDatesOnPosts) }}
<span class="post-date">{{ .Date.Format "Mon, Jan 2, 2006" }}</span>
{{ end }}

Even if you have 10k posts, all you would need to do is change the ShowDatesOnPosts to false in your config.yaml/toml and all dates would be removed from your end-user.

Gotcha: Note that your page-level variables (in this instance, .Params.showthedate) must be set in lowercase in your templating logic.

Hope that helps. Cheers.

5 Likes

@rdwatters - Thanks for the detailed answer!

1 Like

You can also create a condition with a default value, then on the pages/posts which you do/don’t want to show the date, you can mention it. This method will help you save some time by not adding the variable on every single .md file.

First you add the conditional value with the default value as “true” or “false”. This depends on the statement you are trying to do…

{{ if .Params.showthedate | default "true" }}
<span class="post-date">{{ .Date.Format "Mon, Jan 2, 2006" }}</span>
{{ end }}

On the pages or posts where you don’t want to show the date, you need to add the variable as follows.

+++
title = "About"
description = "Hugo, the world’s fastest framework for building websites"
date = "2019-02-28"
showthedate = false
+++

On the able example, if you don’t mention the showthedate option, it will use the default value, in this case “true”.

I hope this example helps someone.

use {{ with .Params.showthedate }} .. {{end}} it wille be true if you have defined showthedate