As an experienced website operation expert, and with a deep understanding of AnQiCMS template tags and content operation strategies, I am happy to give you a detailed explanationprevArchiveThe function of the tag, and how to cleverly obtain the custom field content of the previous document.


An in-depth analysis of Anqi CMSprevArchiveTag and custom field acquisition strategy

In the template development of AnQi CMS,prevArchiveandnextArchiveTags are commonly used tools for navigating the front and back of documents.They aim to provide a concise and efficient way for visitors to easily jump to the previous or next related content after reading the current article, thereby enhancing user experience and page browsing depth.prevArchiveDoes the tag support directly accessing the custom field content of the previous document in the backend, which is indeed a common question among many developers and operators.

From the design philosophy of Anqi CMS template tags,prevArchiveThe core positioning of tags is to provideLightweight, fast responseNavigation data.This means it will only output some basic and commonly used information from the previous document, so that the template can quickly render the title, link, thumbnail, and other navigation elements of the previous document.

Unfortunately, pass directly through{% prevArchive prev %}This way, you cannot get the custom field content set in the background of the previous document.

Consulttag-prevArchive.mdDocument,prevArchiveThe fields explicitly supported by the tag include:Id(Document ID),Title(Document Title),Link(Document Link),Keywords(Document keywords),Description(Document description),CategoryId(Document Category ID),Views(Document Views),Logo(Cover main image),Thumb(Cover Thumbnail),CommentCount(Number of Comments),CreatedTime(Add Time) andUpdatedTime(Updated time). This list does not include user-defined fields.

Then, does this mean that our needs cannot be met?Of course not.AnQi CMS provides a flexible template tag system, and we can cleverly achieve the goal by combining different tags.

Circumvent the country: throughprevArchiveGet the ID, then usearchiveDetailGet complete information

To obtain the custom field content of the previous document, we need to take a two-step approach:

  1. First step: useprevArchiveLabel to obtain the previous document'sId. prevArchiveThe label returns an object containing basic information about the previous document (such as in{% prevArchive prev %}isprev). This object contains the unique identifier we need—the document ID.
  2. The second step: Use the ID of the previous document obtained, combinedarchiveDetailwith tags to get the complete details of the document, including all custom fields. archiveDetailTags are powerful tools for obtaining detailed information about any document, it supports accessing through document ID (idSpecify the document to be queried by a parameter, and it can return complete data including all custom fields.

Let's demonstrate this process with a specific code example:

{# 1. 使用 prevArchive 标签,将上一篇文档数据赋值给 prev 变量 #}
{% prevArchive prev %}
  {% if prev %}
    {# 确保存在上一篇文档,然后获取其ID #}
    {% set prevDocId = prev.Id %}

    {# 2. 使用 archiveDetail 标签,通过 prevDocId 获取上一篇文档的完整数据 #}
    {% archiveDetail fullPrevDoc with id=prevDocId %}
      <h3>上一篇:<a href="{{ fullPrevDoc.Link }}">{{ fullPrevDoc.Title }}</a></h3>
      <p>常规描述:{{ fullPrevDoc.Description }}</p>

      {# 直接通过变量名访问自定义字段,例如假设您有一个名为 'author' 的自定义字段 #}
      {% if fullPrevDoc.author %}
        <p>作者(自定义字段):{{ fullPrevDoc.author }}</p>
      {% endif %}

      {# 如果您需要循环显示所有自定义字段,可以使用 archiveParams 标签 #}
      {% archiveParams prevDocCustomParams with id=prevDocId %}
        {% for param in prevDocCustomParams %}
          {# 这里显示自定义字段的名称和值 #}
          <p>{{ param.Name }}:{{ param.Value }}</p>
        {% endfor %}
      {% endarchiveParams %}
    {% endarchiveDetail %}
  {% else %}
    <p>没有上一篇了。</p>
  {% endif %}
{% endprevArchive %}

In this code, we first useprevArchivegotprevan object and extracted from itprev.Id. Then, we passed this ID toarchiveDetaillabel'sidthe parameter and assigned the complete document data obtained tofullPrevDoc. At this point,fullPrevDocThe object includes all the information from the previous document, including the various custom fields defined in the backend content model. You can accessarchiveDetailReturns the current document fields as is, passes through directlyfullPrevDoc.您的自定义字段名To access them. If needed, you can even pass through them.archiveParamsThe label is retrieved again based on ID and loops through all custom fields to achieve more flexible display.

This method, although it adds an extra step compared to direct access, fully utilizes the flexibility and powerful functions of the Anqi CMS template tags, ensuring that you can obtain any data you need.In practice, due to the excellent performance of AnQi CMS, which is developed based on Go language, even with an additional query operation, it usually will not cause a significant impact on the website's loading speed.


Frequently Asked Questions (FAQ)

  1. Q: Why?prevArchiveWhy does the label not provide all fields directly, including custom fields?A:prevArchiveThe design intention of the label is to provide quick and lightweight navigation data.Under most circumstances, the previous/next article navigation only needs to display the title, link, and other basic information.If the default loading of all fields (including potentially a very large number of custom fields) is enabled, it will increase the complexity of database queries and the amount of data transfer, thereby affecting the rendering efficiency of the page.prevArchiveandarchiveDetailThe function, Anqi CMS can provide **performance in different scenarios.

  2. Q: Can IarchiveDetailDo you get the information of the previous document directly through the document title or URL alias in the tag, instead of the ID?A: Yes, you can.archiveDetailTags not only supportidParameters, but also support throughtoken(The URL alias of the document) to obtain document details. Therefore, if you can obtain the URL alias of the previous document,prevArchiveyou can pass it to the next document as well,archiveDetailoftokenParameters are used to obtain complete information. However, obtaining the ID is usually the most direct and efficient way, as the ID is the primary key in the database.

  3. Q: If the previous document I retrieve does not contain the content of a custom field, will the template report an error?A: Usually, it will not report an error. If throughfullPrevDoc.您的自定义字段名The custom field being accessed has no value (for example, not filled in on the backend), it will return an empty string or zero value without causing any template parsing error. To better control the display, you can use it in the template.{% if fullPrevDoc.您的自定义字段名 %}Make a judgment, only display the field when there is content to keep the page tidy.