UsefulprevArchiveTag: Deep dive into the time mystery of the previous document of the security CMS

prevArchiveTag, especially how to flexibly obtain the release of the previous document (CreatedTime) or update time (UpdatedTime),and present it in a readable format on your website.

In the Anqi CMS,prevArchiveA label is a very practical tool that allows you to easily access the document data that precedes it in the context of the current document. Its usage is simple and clear, usually you just need to declare a variable in the template, for example{% prevArchive prev %}. ThisprevThe variable will later carry various information from the previous document, including document ID, title, link, etc.

However, unlike directly displaying the document title or link, time information inprevArchive标签中是以原始的 Unix 时间戳(timestamp)形式提供的,具体通过 Englishprev.CreatedTimeandprev.UpdatedTimeThese fields are retrieved.These timestamps are the number of seconds elapsed since 00:00:00 UTC on January 1, 1970. For ordinary users, such a series of numbers is hard to understand.

In order to convert these original timestamps into the daily readable date format, Anqi CMS provides a very convenient built-in feature ——stampToDateLabel.This tag is specifically used for formatting timestamps, it requires two parameters: the first is the timestamp you want to format, and the second is the date and time format you expect.2006-01-02 15:04:05Define the format. This means that if you want to display "year-month-day", you enter2006-01-02If you want to display "hour:minute", you enter15:04. Continue in this manner.

Now, let's demonstrate how to retrieve and format the publish or update time of the previous document with a few practical examples.

We assume that we want to display the title and publication date of the previous article at the bottom of the article detail page, and we can write the template code as follows:

{% prevArchive prev %}
  {% if prev %}
    <p>上一篇:
      <a href="{{ prev.Link }}">{{ prev.Title }}</a>
      (发布于:{{ stampToDate(prev.CreatedTime, "2006年01月02日") }})
    </p>
  {% else %}
    <p>没有更早的文章了。</p>
  {% endif %}
{% endprevArchive %}

In this code, we first use{% prevArchive prev %}to get the previous document. Then,{% if prev %}Check if there is a previous document to avoid displaying errors when there is none. The core part is{{ stampToDate(prev.CreatedTime, "2006年01月02日") }}Here we willprev.CreatedTime(the timestamp of the previous document's release) passed instampToDate,and specified the format as "2006-01-02

If you are more concerned about the latest updates of the document, and want to display the update time of the previous document, just need toCreatedTimewithUpdatedTimeand can be adjusted according to need, for example, displayed as a format including specific time:

{% prevArchive prev %}
  {% if prev %}
    <p>上一篇:
      <a href="{{ prev.Link }}">{{ prev.Title }}</a>
      (最后更新:{{ stampToDate(prev.UpdatedTime, "2006-01-02 15:04:05") }})
    </p>
  {% else %}
    <p>没有更早的文章了。</p>
  {% endif %}
{% endprevArchive %}

Here, your users can easily see the detailed publication or update time of the previous document. This is very helpful for judging the freshness of the content or finding the latest relevant information.In content operation strategy, clearly displaying time information can not only enhance the authority of the website content, but also indirectly encourage users to explore more updated content, thus improving the overall user stickiness.

In summary, the Anqi CMS'sprevArchiveTag combinationstampToDateFormat function, providing you with a flexible and powerful way to handle and display the time information of the previous document.Whether it is the update time accurate to the second or the concise and clear release date, it can be achieved through simple template code, making your website content operation more refined.


Common Questions (FAQ)

Q1:nextArchiveDoes the tag also support obtaining the publication or update time of the next document in the same way?A1: Yes,nextArchiveTags andprevArchiveThe function and usage of the label are very similar. You can use it to get the data of the next document.{% nextArchive next %}to get the data of the next document, and then use it similarly.next.CreatedTimeandnext.UpdatedTimeFields, and combine.stampToDatetag for formatting, for example{{ stampToDate(next.CreatedTime, "2006-01-02") }}.

Q2: If the current document is the first one in this category,prevArchiveWhat will the tag return? How should I handle this situation?A2: If the current document is the first article in its category, meaning there is no previous document,prevArchiveLabel-defined variable (such asprev) will be empty. To avoid template rendering errors, you should always use conditional judgments (such as{% if prev %}{% else %}{% endif %}Check if the previous document exists first, and then display the content, just like we did in the article example.

Q3: Besides the publication or update time, can I also doprevArchiveLabel to get other information from the previous document?A3:prevArchiveLabel can provide various information from the previous document, so that you can use it flexibly in the template. In addition toCreatedTimeandUpdatedTime, it also supports getting the document ID (Id) and the title (Title) and the link (Link) Keywords (Keywords) and the description (Description) category ID (CategoryId) views (Views) Cover first image (Logo)、English cover thumbnail (Thumb)、Comment count (CommentCount) etc. You can call these data according to your actual needs, throughprev.字段名in the form of.