Hugo is my static site generator of choice. Today, I wanted to share how I display content on specific dates in Hugo. Let's dig in! Some examplesI sometimes share date-specific messages around sales or events.
Rather than manually publish those on the day they start and manually remove them on the day they end, I set them up ahead of time, and have them only show up between specific dates. Yesterday, I shared how I added the Pride border to my website for Pride Month.
I now have that setup to automatically get show up on June 1 and revert to the normal gray border on July 1. Dates in HugoThere are few little developer Lego pieces in Hugo that I use to make this work.
Using them together, I'm able to get the month, year, or a specific date string that I can compare against. Hugo uses a really weird syntax for formatting date strings. I'm not sure if this is a GoLang thing specifically, or just a Hugo thing. In PHP, you'd pass in For example, to get the current month name, you do this…
Today, June 4, that function would return If I wanted the month as a digit, I'd do this…
And get back Similarly, years always use Comparing datesLet's look at how you can use date strings to compare the current date to some window where content should be shown. Hugo has a few functions for comparing values…
Only show content during a specific monthFor Pride Month, I want to add the
To do that, I get the name of the current month, and check if it's equal (
Every year in June, the class gets added. Every other month of the year, it does not. Only show content between specific datesLet's imagine I'm launching a course on July 1, and I want to run a sale starting on launch day and lasting one week: July 1 through July 7. For that, I can specific my start and end dates in a
I can compare that to my start date ( For this one, I use numbers instead of strings, and use Hugo's
My message will show up on July 1, 2025, and disappear on July 8. It will not repeat on any future year. Rebuilding the siteHugo is, of course, a static site generator. Unlike WordPress or some other dynamic CMS, the HTML is generated ahead of time, and does not run dynamically whenever someone requests a page. The final piece to making these date specific changes work is that I have my site setup to automatically rebuild itself every day at midnight. I have a whole separate article on how to do that, but the short version is…
This is also how I schedule blog posts ahead of time and them automatically go live. Zach Leatherman (creator of another great SSG, 11ty) is the one who taught me this trick. Like this? A Go Make Things membership is the best way to support my work and help me create more free content. Cheers, Want to share this with others or read it later? View it in a browser. |
0 Komentar untuk "[Go Make Things] How to show content on specific dates in Hugo"