Added sorting buttons by Publish or Update date on the home page
Introduction
Sometimes, when browsing a website, you just want to see the most recently updated articles. However, since this site uses Hugo, which is a static site generator, the article information on the homepage is fixed at the moment the site is generated, making dynamic switching impossible. In this case, we can generate a page similar to the homepage and use buttons to switch between different views, achieving a dynamic effect to implement article sorting functionality.
Adding a Toggle Button
We can add a toggle button anywhere on the homepage. Here, we use a switch similar to the one below, adding animations, and placing the button at the top of the article section.
The site currently uses the LoveIt theme. We can find the homepage HTML file index.html in the /layouts/ directory of the site’s root folder. Then, add the following code above the {{- /* Posts */ -}} section at the beginning of the articles.
| |
Now, you can see the toggle button on the homepage! If you want to use a different type of button, you can replace it here as well.
Modifying the Internationalization Language File
If you open the homepage, you’ll notice that the text before the button seems incorrect. This is because the translation key has not yet been added to the language file. We can follow the steps below to add the translation key.
Navigate to the /i18n/ directory in the website’s root folder, find the corresponding language file, and add the publishDate_updateDate key-value pair. Below is an example using Traditional Chinese (zh-TW) and English (en).
zh-TW.toml
| |
en.toml
| |
This will make the text before the button appear correctly.
Adding the index.lastmod.html Template
After clicking the button, you may notice that nothing happens. This is because we haven’t added the pages for the different sorting options. To fix this, we can copy the /layouts/index.html homepage file and paste it in the /layouts/posts/ directory in the website’s root folder, then rename it to index.lastmod.html. Then, in the {{- /* Posts */ -}} section, add {{- $pages = $pages.ByLastmod.Reverse -}} to sort the posts by the update time. The specific changes are as follows.
| |
Add Article and Modify to Homepage
Finally, to complete the setup, we simply need to create an article and specify the newly created template.
In the website’s root directory, navigate to the /content/ folder, create a homepage.lastmod folder, and add the _index.language code.md file. Then, insert the following content.
| |
If it’s a multilingual website, make sure to add the corresponding _index.language code.md file for each language. The content to fill in will be the same. For example, the files added to this site are as follows.
| |
With this, we now have the functionality to display articles in different orders based on the publish date or update date.
Conclusion
I have wanted to implement this feature for a long time, but after much research, I couldn’t figure out how to bypass the static site generation structure. One day, I thought of generating a webpage with a different sorting order and using hyperlinks to navigate back and forth. After searching online, I found a feasible solution and finally completed the modification.
Environment
- Hugo 0.144.2
- LoveIt theme (version from GitHub on February 21, 2025)
References
- Two Home Pages. (2021, March). HUGO. Retrieved on March 28, 2025. https://discourse.gohugo.io/t/two-home-pages/31312/9

![[Test] Website Network Connection Speed Test](https://josh-test-lab.github.io/posts/Website%20Network%20Connection%20Speed%20Test/cover%20image.webp)
![[Thoughts] 2024 Joint Conference of University Administrators for Teacher Education](https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/2024%20Joint%20Conference%20of%20University%20Administrators%20for%20Teacher%20Education/cover%20image.webp)



