How to effectively organize and present content in website construction and operation is the key to improving user experience and content discovery efficiency.For friends using AnQiCMS, it is a very practical feature to flexibly sort the document list.Whether it is to allow visitors to see the most popular articles first or the latest updates, AnQiCMS provides a simple and powerful way to achieve this.

Generally, when we want to display a series of documents, such as blog posts, product introductions, or news lists, we would like them to be arranged in a specific logical order.The most common requirement is to sort documents by the number of views (popularity) or publication time (newest).Aqin CMS provides special template tags for this, allowing you to easily implement these sorting logic on the website front-end.

Core Tool:archiveListDocument List Tags

In the template system of Anqi CMS,archiveListThe tag is a core tool for retrieving document lists. It is powerful and can be used to filter, limit, and sort the documents you want to display with various parameters.All sorting logic will be centered around this tag'sorderParameter expansion.

The basic usage form of this tag is as follows:

{% archiveList 变量名称 with 参数="值" %}
    {# 在这里循环显示文档内容 #}
{% endarchiveList %}

Among them,变量名称Can be a custom name, for examplearchivesUsed to refer to each document in a loop.参数andIs the key to controlling the behavior of the document list.

Sort option one: Sort by views (Views)

If your goal is to let visitors see the most popular and most read content on the website at a glance, then sorting in descending order of views is a very good choice. Anqi CMS'sarchiveListTags providedviews descThis parameter value is used to implement this function.descMeans descending order, that is, from high to low.

For example, if you want to display the top 10 viewed articles on your website homepage or specific category page, you can write the template code like this:

<div class="most-popular-articles">
    <h2>热门文章</h2>
    <ul>
        {% archiveList archives with type="list" order="views desc" limit="10" %}
            {% for item in archives %}
            <li>
                <a href="{{ item.Link }}" title="{{ item.Title }}">
                    <h3>{{ item.Title }}</h3>
                    <p>浏览量:{{ item.Views }}</p>
                </a>
            </li>
            {% endfor %}
        {% endarchiveList %}
    </ul>
</div>

In this code block:

  • type="list"Indicates that we are getting a regular list, not a list for pagination.
  • order="views desc"Explicitly specifies sorting by the document's view count (Views) in descending order.
  • limit="10"It limited the display to only the top 10 most popular articles.
  • Inforinside the loop,item.LinkGet the document link,item.TitleGet the document title,item.ViewsIt shows the document's page views.

With such settings, your website will dynamically display the content that most attracts the attention of visitors.

The second sorting method: Sort by publish time (CreatedTime)

For news updates, latest product releases, or time-sensitive content, we tend to display them in the order from new to old based on the publication time, so that visitors can stay informed about the latest information. In Anqi CMS, the documentsIdIt is usually automatically incremented, therefore sorted according toid descThis can effectively achieve the sorting from new to old by publication time.

To display the latest documents, you can modify it in this wayarchiveListTags:

<div class="latest-articles">
    <h2>最新动态</h2>
    <ul>
        {% archiveList archives with type="list" order="id desc" limit="5" %}
            {% for item in archives %}
            <li>
                <a href="{{ item.Link }}" title="{{ item.Title }}">
                    <h3>{{ item.Title }}</h3>
                    <p>发布时间:{{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}</p>
                </a>
            </li>
            {% endfor %}
        {% endarchiveList %}
    </ul>
</div>

Here:

  • order="id desc"Tell the system to sort documents by ID in descending order, as IDs are usually assigned in the order of document creation, which is equivalent to sorting by the latest release time.
  • item.CreatedTimeGet the creation time of the document, which is a timestamp.
  • stampToDate(item.CreatedTime, "2006-01-02 15:04")It is a very useful helper function, which can format the timestamp into a date and time string that is easier for us to read."2006-01-02 15:04"It is the time formatting standard in Go language, you can adjust it as needed to display year-month-date, hour:minute, and other information.

Combined with pagination function usage.

When your document quantity is very large, simply displaying a list is not enough, and pagination features are usually required.archiveListin the tag, you just need totypethe parameter topage, then combinepaginationtag to achieve.

For example, a list of the latest articles with pagination can be implemented in this way:

<div class="paginated-latest-articles">
    <h2>所有文章</h2>
    <ul>
        {% archiveList archives with type="page" order="id desc" limit="10" %}
            {% for item in archives %}
            <li>
                <a href="{{ item.Link }}" title="{{ item.Title }}">
                    <h3>{{ item.Title }}</h3>
                    <p>发布时间:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</p>
                </a>
            </li>
            {% endfor %}
        {% endarchiveList %}
    </ul>

    {# 分页代码 #}
    <div class="pagination-controls">
        {% pagination pages with show="5" %}
            {% if pages.PrevPage %}<a href="{{ pages.PrevPage.Link }}">上一页</a>{% endif %}
            {% for page in pages.Pages %}
                <a href="{{ page.Link }}" class="{% if page.IsCurrent %}active{% endif %}">{{ page.Name }}</a>
            {% endfor %}
            {% if pages.NextPage %}<a href="{{ pages.NextPage.Link }}">下一页</a>{% endif %}
        {% endpagination %}
    </div>
</div>

Here, type="page"TellarchiveListThe label indicates that this is a list that requires pagination and:limit="10"defines that 10 documents are displayed per page. Following that,paginationTags will automatically generate page number links, convenient for visitors to browse all pages.

Summarize the operation steps

Sort the document list in Anqi CMS by page views or publish time, just follow these steps:

  1. Confirm the template file to be modifiedThis is usually the homepage template of the website (index/index.htmlcategory list page template ({模型table}/list.htmlor other parts containing document lists.
  2. FindarchiveListTag: Locate the code block responsible for displaying the document list in the template file.{% archiveList ... %}code block."),
  3. AdjustmentorderParameter:
    • To pressView count from high to lowSort, please setorderthe parameter toorder="views desc".
    • To pressPublished time from new to oldSort, please setorderthe parameter toorder="id desc".
  4. Save and update cacheSave the template file, if the website has enabled caching, remember to update the system cache in the background so that the new sorting rules take effect.

The template tag design of Anqi CMS makes content sorting intuitive and easy to use. By simply adjusting the parameters, your website content can be presented in the most suitable way for visitors, greatly enhancing the practicality and user experience of the website.


Frequently Asked Questions (FAQ)

  1. Ask: Can I sort the document in ascending order (asc) besides descending order (desc)?Of course you can. Inorderthe parameters, besidesdesc(descending order), you can also useasc(Ascending). For example,order="views asc"It will be sorted from low to high by views, whileorder="id asc"it will be sorted by the earliest published documents.

  2. Question:order="id desc"andorder="CreatedTime desc"What is the difference, which one should I choose to sort by publication time?Answer: In AnQi CMS,orderthe parameter supports directlyid descto sort by document ID in descending order. Since the document ID is usually a unique and incrementing identifier automatically assigned when the system is created, thereforeid descEffectively realize the sorting from new to old by publishing time.CreatedTimeThe field itself stores the publishing time.