In website content operation, the publication time of the article is a seemingly simple but crucial detail.It not only affects users' judgments on the timeliness of content, but is also an important reference factor for search engines to evaluate content freshness and ranking.For AnQiCMS users, being able to flexibly display and customize the publication time of articles in templates is a basic operation to enhance website user experience and SEO performance.
AnQiCMS as an efficient and customizable content management system, provides a very friendly template engine, allowing you to easily control the way content is displayed.Display the publication time of the article and customize its format, which is achieved by using its template tags and filters.
Get the publication time of the article: Basic fieldCreatedTimewithUpdatedTime
Firstly, we need to understand how the publishing time of the article is stored in AnQiCMS. In the document model of AnQiCMS, each article is built-in withCreatedTime(creation time) andUpdatedTime(Update time) These fields. When you publish or edit articles in the background, these times will be automatically recorded.
In the AnQiCMS template, whether througharchiveListTag to loop display article list or use it on the article detail pagearchiveDetailTag to get information of a single article, you can access these time fields. For example, when you are in an article loop,itemRepresents the current article object, so its publication time isitem.CreatedTime; If on the article detail page,archiveRepresents the current article object, its publication time isarchive.CreatedTime.
Output directly in the template{{archive.CreatedTime}}or{{item.CreatedTime}}When you find it, it displays a series of numbers, which is what is called a timestamp.A timestamp is usually the number of seconds that have elapsed since a fixed point in time (such as January 1, 1970, 0:0:0) to the current time.Although machines can understand this format, it is not intuitive for visitors.Therefore, we need to convert it to an easily readable date and time format.
Custom time format:stampToDateMagic of tags
AnQiCMS provides a very practical template tag for this——stampToDate. This tag's 'magic' lies in its ability to easily convert the original timestamp into any date and time format you want.
stampToDateThe usage method of the tag is very intuitive:{{stampToDate(时间戳, "格式")}}. The 'timestamp' mentioned earlier is this.archive.CreatedTimeoritem.CreatedTimeThe field value. The "format" parameter is its core secret, which follows the time formatting rules of the Go language (the development language of AnQiCMS).
The date formatting rule in Go language is different from other programming languages (such as PHP or Python). It does not useY-m-dor%Y-%m-%dThis placeholder is instead with a specific reference date and time as a template:2006-01-02 15:04:05. You need to replace the numbers in this reference date with the format you want to see in the final output.
For example:
- To display the full year (2006), use
2006. - To display the two-digit month (01), use
01. - To display the two-digit date (02), use
02. - Use to display the hour (15) in 24-hour format
15. - Use to display the minute (04)
04. - Use to display the second (05)
05.
Through this reference template, you can flexibly combine various formats:
- Only display the date (year-month-day):
{{stampToDate(archive.CreatedTime, "2006-01-02")}} - Display Chinese date:
{{stampToDate(archive.CreatedTime, "2006年01月02日")}} - Display date and time:
{{stampToDate(archive.CreatedTime, "2006-01-02 15:04")}} - Only display time:
{{stampToDate(archive.CreatedTime, "15:04")}} - Show Abbreviated English Month Names:
{{stampToDate(archive.CreatedTime, "Jan 02, 2006")}} - Show the day of the week:
{{stampToDate(archive.CreatedTime, "Mon Jan 02 2006")}}
Practice in the template: Show publish time
Next, let's see how to apply it in the actual templatestampToDate.
1. Show publish time on the article detail page
In the article detail page (for examplearchive/detail.htmlOr a custom article template), you would typically access the current article object directlyarchiveYou can display the publish time like this:
<article>
<h1>{{ archive.Title }}</h1>
<p>
发布于:{{ stampToDate(archive.CreatedTime, "2006年01月02日 15:04") }}
{%- if archive.UpdatedTime and archive.UpdatedTime > archive.CreatedTime %}
(更新于:{{ stampToDate(archive.UpdatedTime, "2006年01月02日 15:04") }})
{%- endif %}
</p>
<div>
{{ archive.Content|safe }}
</div>
</article>
In the code snippet above, we first displayed the article'sCreatedTime. Then, we added a conditional judgment: ifUpdatedTimeexists and is later thanCreatedTime, then display the "Updated at" time, so that visitors can understand the latest status of the content. Here|safeThe filter is used to ensure that HTML tags in the article content are parsed and displayed correctly.
2. Display the publish time on the article list page.
In the article list page (for examplearticle/list.htmlOr a custom category list template, you usually usearchiveListTags to loop through multiple articles. Inside the loop body,itemThe variable represents each article.
<ul class="article-list">
{% archiveList archives with type="page" limit="10" %}
{% for item in archives %}
<li>
<h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
<p class="meta">
发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}
<span>浏览量:{{ item.Views }}</span>
</p>
<p>{{ item.Description }}</p>
<a href="{{ item.Link }}" class="read-more">阅读更多</a>
</li>
{% empty %}
<li>目前还没有文章发布。</li>
{% endfor %}
{% endarchiveList %}
</ul>
In this example, the publication time of each article is displayed below the title in a concise "Year-Month-Day" format, helping users to quickly browse.
More custom formats and practical skills
MasterstampToDateTags and Go language's time formatting rules, you will have unlimited customization capabilities. You can try:
- Internationalized date format:If your website is aimed at global users