Understand the publication and update time of an article, for readers, it not only helps to judge the timeliness of the content and avoid outdated information, but also is an important consideration for search engine optimization (SEO).AnQiCMS as an efficient content management system, provides flexible template features, allowing you to easily display these key time information on your website.
AnQiCMS's template system uses syntax similar to Django, it is through double curly braces{{变量}}output the value of the variable, while logical control (such as conditional judgment, loop, etc.) is used{% 标签 %}structure. In AnQiCMS, the publishing time of the article (CreatedTimeAnd update time(UpdatedTimeTimestamps are stored in the form. To convert these timestamps into the date and time format we are familiar with, AnQiCMS provides a very useful built-in tag:stampToDate.
Display the publication and update time on the article detail page
When you are browsing the detail page of a single article, you will usually usearchiveDetailLabel to get the detailed information of the current article. This label is very powerful and can directly access various attributes of the article.
To display the publication time of the article, you can use the following method:
发布时间:{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04") }}
Herearchive.CreatedTimeThis is the timestamp of the current article's publication.stampToDateThe second parameter of the tag"2006-01-02 15:04"It is a formatted string that tells the system the format you want the time to be displayed as 'Year-Month-Day Hour:Minute'.
Similarly, to display the update time of the article, you can do it like this:
更新时间:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04:05") }}
archive.UpdatedTimeRepresents the update timestamp of the article. You can see that by adjusting the formatting string, you can flexibly control the time display to seconds.
Displays the publish time and update time on the article list page.
On the article list page, you will usearchiveListLabel combinationfora loop to display multiple articles. In this case,CreatedTimeandUpdatedTimeit will be used as the attribute of each article object inside the loop (usually named)item).
For example, if you have a list of articlesarchivesyou can iterate through and display the time of each article:
{% archiveList archives with type="page" limit="10" %}
{% for item in archives %}
<article>
<h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
<p>
发布于:{{ stampToDate(item.CreatedTime, "2006年01月02日") }}
{% if item.UpdatedTime > item.CreatedTime %}
(最后更新:{{ stampToDate(item.UpdatedTime, "2006-01-02") }})
{% endif %}
</p>
<p>{{ item.Description }}</p>
</article>
{% endfor %}
{% endarchiveList %}
In this example, we also added a simple judgment, only whenUpdatedTimelater thanCreatedTimeOnly when, the update time is displayed, this can avoid repeating the same time in the case that the article has never been updated.
Time formatting options detailed explanation
stampToDateThe core of the label lies in its formatting parameters. This parameter follows the unique time formatting rules of the Go language, which is not traditional.Y-m-dor%Y-%m-%dInstead, use a specific date as a reference template: "2006-01-02 15:04:05.You just need to remember this reference template, and then replace the date/time part you want to display with the corresponding number in the reference template.
Here are some commonly used formatting examples:
- Date (Year-Month-Day):
"2006-01-02" - Date (Year/Month/Day):
"2006/01/02" - Complete date and time (to seconds):
"2006-01-02 15:04:05" - Date and time (to minutes):
"2006-01-02 15:04" - Time only (Hour:Minute):
"15:04" - Date in Chinese:
"2006年01月02日" - Date and time in Chinese:
"2006年01月02日 15时04分"
Comprehensive example
Here is an example of a template snippet that integrates the publish time and update time into the article display, giving you a more intuitive understanding of how it is applied on the actual page:
<div class="article-meta">
<h1>{{ archive.Title }}</h1>
<p class="time-info">
<span>发布时间:{{ stampToDate(archive.CreatedTime, "2006年01月02日 15:04") }}</span>
{% if archive.UpdatedTime > archive.CreatedTime %}
<span>最后更新:{{ stampToDate(archive.UpdatedTime, "2006年01月02日 15:04") }}</span>
{% endif %}
</p>
<div class="article-content">
{{ archive.Content|safe }}
</div>
</div>
By using the above method, you can easily and flexibly display the publication and update time of articles in AnQiCMS templates, which not only improves user experience but also lays a good foundation for the website's SEO performance.
Frequently Asked Questions (FAQ)
Q1: Why is the time displayed as a long number instead of a date format?A1: This is usually because you forgot tostampToDateProvide the second parameter in the tag, which is the time formatting string.CreatedTimeandUpdatedTimeIt is a timestamp and needs to be converted throughstampToDateto a readable date and time format. Make sure your code is similar.{{ stampToDate(item.CreatedTime, "2006-01-02") }}of which"2006-01-02"Is the format you expect.
Q2: Can I make the publish time only show the date and the update time show to the second? Can this be done?A2: Of course.stampToDateThe flexibility of the label is manifested in its formatting parameters. You can use this format for publish time"2006-01-02"and this format for update time"2006-01-02 15:04:05"This format, the system will convert according to your settings separately.
Q3: What will be displayed as the update time if the article has never been updated?A3: In AnQiCMS, if an article has never been manually updated, itsUpdatedTimefield will usually beCreatedTimethe same. Therefore, if you display it directlyUpdatedTimeIt will be consistent with the release time. To provide a better user experience, you can add a conditional judgment as shown in the article list page example ({% if item.UpdatedTime > item.CreatedTime %}Only when the update time is indeed later than the release time, will the word "Last Updated" be displayed.