In website content operation, the publication time of articles is a seemingly simple but crucial detail.It not only affects the user's judgment of the timeliness of the content, but also is an important reference factor for the search engine to evaluate the freshness of the content and perform ranking.For AnQiCMS users, flexibly displaying and customizing the publishing time of articles in templates is a fundamental operation to enhance the 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 realized by using its template tags and filters.
Get the article publish time: Basic fieldsCreatedTimeWithUpdatedTime
Firstly, we need to understand how the publication time of the article is stored in AnQiCMS. In the document model of AnQiCMS, each article is built in withCreatedTime(Creation time) andUpdatedTimeThese fields are (update time). When you publish or edit articles in the background, these times will be automatically recorded.
Whether in the AnQiCMS template, througharchiveListLabel to display article list in a loop, or use it in the article detail pagearchiveDetailLabel 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 the publication time isitem.CreatedTime; On the article detail page, ifarchiveRepresents the current article object, its publication time isarchive.CreatedTime.
Output directly in the template{{archive.CreatedTime}}or{{item.CreatedTime}}When you check it, you will find it displays a string of numbers, which is what we call a timestamp.The timestamp is the number of seconds that have elapsed since a fixed point in time (such as January 1, 1970, 00:00:00).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.
Customize the time format:stampToDate标签的魔法English
AnQiCMS provides a very practical template tag -stampToDateThis tag's 'magic' lies in its ability to easily convert any original timestamp into the date and time format you want.
stampToDateThe usage of tags is very intuitive:{{stampToDate(时间戳, "格式")}}. The 'timestamp' is what we mentioned earlierarchive.CreatedTimeoritem.CreatedTimeThe content of the field. The "format" parameter is the core secret, which follows the time formatting rules of the Go language (the development language of AnQiCMS).
The date formatting rules in Go language are different from other programming languages (such as PHP or Python). It does not useY-m-dor%Y-%m-%dThis placeholder is not used, but a specific reference date and time is used as a template:2006-01-02 15:04:05. You need to replace the numbers in this reference date with the corresponding date and time format you wish to see in the final output.
For example:
- To display the full year (2006), use
2006. - To display two-digit months (01), use
01. - To display two-digit dates (02), use
02. - To display the hour (15) in 24-hour format, use
15. - To display the minute (04), use
04. - To display the second (05), use
05.
Through this reference template, you can flexibly combine various formats:
- Only show the date (year-month-day):
{{stampToDate(archive.CreatedTime, "2006-01-02")}} - Show 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 English month abbreviations:
{{stampToDate(archive.CreatedTime, "Jan 02, 2006")}} - Display the day of the week:
{{stampToDate(archive.CreatedTime, "Mon Jan 02 2006")}}
Practice in the template: Display publish time
Next, let's see how to apply it in a real templatestampToDateLabel.
1. Display publish time on the article detail page
In the article detail page (for example)archive/detail.htmlOr a custom article template), usually you will directly access the current article objectarchive. You 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 above code snippet, 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 publication time on the article list page.
On article list page (for examplearticle/list.htmlor a custom category list template), you usually usearchiveListto loop through multiple articles.itemvariable 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-date" format, helping users to quickly browse.
More custom formats and practical tips
MasterstampToDateLabels and Go language time formatting rules give you infinite custom capabilities. You can try:
- Internationalized date format:If your website is for global users