How to effectively organize and present content in building and operating a website 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 desired for visitors to see the most popular articles first or the latest updates, AnQiCMS provides a simple and powerful way to achieve this.

English, typically, when we want to display a series of documents, such as blog articles, product introductions, or news lists, we would like them to be arranged in a specific logical order.The most common requirement is to sort according to the document's page views (popularity) or publication time (newest).The AutoCMS provides dedicated template tags, allowing you to easily implement these sorting logic on the website front-end.

Core Tools:archiveListDocument list label

In the template system of Anqi CMS,archiveListLabels are the core tools for retrieving document lists.It is powerful and can filter, limit, and sort the documents you want to display through various parameters.orderParameter expansion.

The basic usage form of this tag is like this:

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

Among them,变量名称It can be a name you customize, for example,archivesused to refer to each document in the loop.参数andis the key to controlling the behavior of the document list.

排序方式一:按浏览量(English)排序

If your goal is to make visitors see the most popular and most read content on the website at a glance, then sorting by views in descending order is a very good choice. The Anqi CMS isarchiveListTags provideviews descThis parameter value is used to implement this function.descIndicates descending order, that is, from high to low.

For example, if you want to display the 10 most viewed articles on your website's homepage or a 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"It indicates that we are getting a regular list, not a list used for pagination.
  • order="views desc"Explicitly specified to sort by the document's views count.ViewsSorted in descending order by (
  • limit="10"Then it limited to only display the top 10 most popular articles.
  • InforInside the loop,item.LinkGet the document link,item.TitleGet the document title,item.ViewsThen it shows the page views of the document.

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

排序方式二:按发布时间(CreatedTime)排序

For news updates, latest product releases, or time-sensitive content, we prefer to display them in the order from new to old based on the publishing time, allowing visitors to stay informed with the latest information. In the AnQi CMS, documents ofIdIt is usually auto-incremented, so it followsid descSorting can effectively achieve the arrangement from new to old according to the publication time.

To display the latest documents, you can modify it like thisarchiveListTags:

<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"Tells the system to sort the documents by document ID in descending order. Since document IDs are typically assigned in the order of document creation, this is equivalent to sorting by the latest publication 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 auxiliary function that can format timestamps into a date and time string that is easier for us to read."2006-01-02 15:04"It is the time formatting standard of Go language, you can adjust it according to your needs to display year-month-day, hour:minute and other information.

Use it in combination with pagination function

When the number of your documents is very large, simply displaying a list is not enough. It usually also requires a pagination feature.archiveListin the tags, you only need totypeparameter settingspagethen combinepaginationthe tag to implement.

For example, a paginated list of the latest articles can be implemented like this:

<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"informarchiveListTags is a paginated list that needslimit="10"defined to display 10 documents per page.paginationLabels will automatically generate page number links for visitors to browse all pages conveniently.

Summarize the operation steps

To sort the document list in AnQi CMS by views or publication time, just follow these steps:

  1. Confirm the template file you need to modify:This is usually the homepage template (index/index.html), category list page template ({模型table}/list.html) or other parts containing document lists.
  2. FindarchiveListtags:Locate the part responsible for displaying document lists in the template file.{% archiveList ... %}code block.
  3. AdjustmentorderParameters:
    • To browseSort by views from high to lowPlease setorderparameter settingsorder="views desc".
    • To browsePublish time from new to oldPlease setorderparameter settingsorder="id desc".
  4. Save and update cache:Save the template file, if the cache is enabled on the website, 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' needs, greatly enhancing the practicality and user experience of the website.


Common Questions (FAQ)

  1. 问:除了降序(desc),我能否按升序(asc)排列文档?答:Of course you can. Inorder参数中,除了desc(降序),您也可以使用asc(升序)。例如,order="views asc"Will be sorted by the number of views from low to high.order="id asc"It will be sorted by the earliest published documents.

  2. Q:order="id desc"andorder="CreatedTime desc"What is the difference, and which should I choose to sort by publication time?答:In the Anqi CMS,orderparameters directly supportid descsort according to the document ID in descending order. Since the document ID is usually a unique and increasing identifier automatically assigned when the system is created, thereforeid descIt can be implemented very effectively to sort by release time from new to old.CreatedTimeThe field itself stores the release time