How `categoryList` and `archiveList` work together to display the latest few documents under each category?

Calendar 👁️ 70

As an experienced website operations expert, I know the power of a content management system (CMS), not only in the perfection of its backend functions, but also in the flexibility of front-end content display.AnQiCMS (AnQi CMS) is just such an outstanding tool, based on the high-performance architecture of the Go language, with its concise and efficient template tag system, it allows us to easily handle all kinds of complex content display needs.

Today, let's delve into the two core tags of AnQiCMS -categoryListandarchiveList——How to cleverly collaborate to help us accurately display the latest few documents under each category of the website, thereby greatly enriching the page content and enhancing the user experience.

1. Understanding Foundation: Category List TagscategoryList

In AnQiCMS,categoryListTags are a powerful tool for managing and displaying the structure of a website's categories. They allow us to retrieve category information under specific content models, whether it's articles, products, or other custom models, and can be flexibly called through them.

When we usecategoryListWhen, you can specify some key parameters to control its behavior, such as:

  • moduleId: Tell the system which content model (such as article model ID 1, product model ID 2) we want to get the category for.
  • parentId: Used to build the category hierarchy,parentId="0"Indicates retrieving all top-level categories, while specifying a category ID can retrieve its subcategories.
  • limit: Limits the number of categories returned, such as displaying only the first 10 categories.

This tag will return an array of category objects that contain information such as category ID (Id), category title (Title), category link (Link), whether there is a subcategory (HasChildren) and other key information. This information will serve as the bridge for us to obtain documents under categories later on.

2. Content Core: Document List LabelarchiveList

With categories, it is naturally necessary to display the specific content under the category.archiveListTags are the core tools used in AnQiCMS to retrieve document lists (or called archives, such as articles, products, etc.).It can meet the needs of various scenarios from conventional lists, related documents to pagination display.

archiveListIts strength is also reflected in its rich parameter configuration, among which are those closely related to today's topic:

  • categoryId: This is a connectioncategoryListThe key, through this parameter, we can specify which category of documents to retrieve.
  • limit: Controls how many documents to display under each category, for example, 'the latest 5 articles.'
  • order: Determines the document sorting method. To achieve 'latest' display, we usually useorder="id desc"(Sorted by document ID in descending order, i.e., the latest released at the top) ororder="createdTime desc"(Sorted by creation time in descending order). If you want to display popular documents, you can useorder="views desc"(Sorted by views in descending order).
  • typeIn order to display a fixed number of documents on unclassified list pages (such as the homepage or a specific module), we generally willtypeis set to"list".

archiveListIt will return an array of document object, each containing the document ID(Id),Title(Title), link (Link), the abstract. (Description),release time(CreatedTime)as well as the thumbnail (Thumb)and other useful information.

3. Collaborative work: Display the latest document under each category

Now, we willcategoryListandarchiveListThese tags cleverly combine to achieve our goal: displaying the latest few documents under each category.

The core idea is:First usecategoryListIterate through all categories that need to be displayed, and then, within each iteration of the loop, nest another loop usingarchiveListand thencategoryIdbind it dynamically to the current category ID and setlimitandorderThe parameter is used to retrieve the latest documents in a specified quantity.

Let's understand this process through a specific template code example:

{# 外层:使用 categoryList 获取指定模型下的所有顶级分类 #}
{% categoryList categories with moduleId="1" parentId="0" %}
    {# 遍历每个获取到的分类 #}
    {% for category in categories %}
        <section class="category-block">
            <h3>
                <a href="{{ category.Link }}">{{ category.Title }}</a>
                <small>(共 {{ category.ArchiveCount }} 篇文章)</small>
            </h3>
            <ul class="latest-articles-list">
                {# 内层:嵌套使用 archiveList 获取当前分类下的最新文档 #}
                {# categoryId 动态绑定为当前分类的 Id #}
                {# limit 限制只显示最新的 5 篇文章 #}
                {# order="id desc" 确保按 ID 降序,即最新发布在前 #}
                {% archiveList archives with type="list" categoryId=category.Id limit="5" order="id desc" %}
                    {# 遍历当前分类下的每篇最新文档 #}
                    {% for article in archives %}
                        <li>
                            <a href="{{ article.Link }}">{{ article.Title }}</a>
                            <span class="publish-date">{{ stampToDate(article.CreatedTime, "2006-01-02") }}</span>
                        </li>
                    {% empty %}
                        {# 如果该分类下没有文档,则显示友好提示 #}
                        <li class="no-content">该分类下暂时没有文档发布。</li>
                    {% endfor %}
                {% endarchiveList %}
            </ul>
        </section>
    {% endfor %}
{% endcategoryList %}

Code analysis:

  1. {% categoryList categories with moduleId="1" parentId="0" %}This line of code will retrieve the article model(moduleId="1") Under all top-level categories (parentId="0"), and store the results incategoriesthe variable.
  2. {% for category in categories %}: We start traversing these top-level categories, each loop,categoryvariables represent an independent category object.
  3. <h3><a href="{{ category.Link }}">{{ category.Title }}</a></h3>Here shows the current category title and link, convenient for users to click and view more content.
  4. {% archiveList archives with type="list" categoryId=category.Id limit="5" order="id desc" %}This is the key to realizing the core function. Here,categoryIdis dynamically set to the currentcategoryofId.limit="5"Tell the system that we only need the latest 5 documents, andorder="id desc"which ensures that these documents are arranged in the order from new to old.type="list"Then we ensure that we are getting a fixed number of lists, not data for pagination.
  5. {% for article in archives %}: We continue to iterate through the latest documents obtained under the current category.
  6. <li><a href="{{ article.Link }}">{{ article.Title }}</a>...</li>: Display the title, link, and usage of each documentstampToDateThe formatted publish date after the filter.
  7. {% empty %}: It is a very practical tag, whenarchiveList(or any for loop) when no documents are retrieved,emptyThe content in the block will be displayed. This can effectively avoid blank areas on the page, providing a better user experience.

By using this nested calling method, we can present the latest dynamic of each category in a structured and beautiful way on the homepage of the website, a specific topic page, or any page that needs to aggregate multiple categories of content, greatly enhancing the information density and attractiveness of the website.

4. The actual scenario and application value

ThiscategoryListwitharchiveListCollaborative work mode is widely used in the operation of actual websites:

  • Dynamic area on the homepage of the websiteIt is easy to build modules such as 'Latest News', 'Product Updates', 'Industry Dynamics', etc., each corresponding to a category and displaying the latest information under that category.
  • Topic page aggregation: A special page created for a specific topic, which can aggregate the latest content of multiple related categories, allowing users to obtain the information they need in one-stop.
  • Sidebar content recommendationIn the category list page sidebar, you can display documents under the current category such as 'most popular clicks' or 'newest releases' to guide users to deeper browsing.

This flexible content combination ability not only meets the diverse display needs of content operators, but also optimizes the website's SEO structure, increases internal links, and ultimately improves the user's browsing experience and the professionalism of the website.

5. Frequently Asked Questions (FAQ)

Q1: How do I display the latest documents for a specific category instead of all categories?

A1:If you only need to display the latest documents under a specific category without traversing all categories, you can directly omit the outer layercategoryListtag, used alonearchiveListFor example, to display the latest 5 documents under the category with ID 10, you can write it like this:

<ul class="single-category-latest">
    {% archiveList archives with type="list" categoryId="10" limit="5" order="id desc" %}
        {% for article in archives %}
            <li><a href="{{ article.Link }}">{{ article.Title }}</a></li>
        {% empty %}
            <li class="no-content">该分类下暂无最新文档。</li>
        {% endfor %}
    {% endarchiveList %}
</ul>

Q2:archiveListoforderParameter, excluding:id descWhat are some commonly used sorting methods?

A2: orderThe parameters are very flexible, you can choose different sorting fields and order according to your needs:

  • id desc: Sort by document ID in descending order, which is usually equivalent to the most recently released (by default, the ID is incremented).
  • createdTime desc: Arrange documents in descending order of creation time, which is also a commonly used method to obtain the latest documents.
  • views desc: Arrange documents in descending order of views, used to display popular or most popular documents.
  • sort desc: Sort by the custom sorting field in descending order. If the backend has set a custom sorting value for the document, it can be sorted by this value.You can choose the most suitable sorting method according to your actual needs.

Q3: What will be displayed on the page if there are no documents under a certain category? How can blank areas be avoided?

A3:As shown in the example article, whenarchiveList(or otherforThe AnQiCMS template engine does not find any documents matching the conditions in the loop{% empty %}tags come into play. In{% for ... %}and{% endfor %}You can add between them{% empty %}Please place the prompt information after the loop content is empty.This can effectively avoid the appearance of a large blank area on the page, providing a more friendly user experience, for example: 'There are no latest documents under this category.'Or 'Content is being updated, please wait!'

Related articles

Can the `categoryList` tag return the `item.Description` field for SEO description on the category page?

In website operation, the SEO description (Meta Description) of the category page is one of the key elements to improve search engine ranking and click-through rate.A well-written description that not only accurately summarizes the page content but also attracts users to click.For website administrators and content operators using AnQiCMS, how to efficiently and accurately utilize the system functions to manage these descriptions is a common issue.Today, let's delve deeply into a specific issue: `categoryList` tag returns the `item'

2025-11-06

How to highlight the category link being accessed according to the `item.IsCurrent` field?

As an experienced website operations expert, I know that user experience is the foundation of website success.How to help visitors quickly locate their current position on a content-rich website and clearly understand the navigation structure is the key to improving user satisfaction.Today, let's delve deeply into a very practical and elegant feature of Anqi CMS - how to cleverly highlight the current category link accessed using the `item.IsCurrent` field, thereby significantly optimizing the navigation experience of the website.

2025-11-06

What is the actual use of the `item.Spacer` field in multi-level category display, and how to beautify the prefix?

As an experienced website operations expert, I know that every seemingly minor feature in a content management system may play a key role in user experience and content presentation.Today, let's delve deeply into a frequently overlooked but indispensable field in AnQiCMS, which is essential for building a clear and hierarchical content structure - `item.Spacer`.

2025-11-06

How to display a category thumbnail (`item.Thumb`) or Banner image (`item.Logo`) next to the category list?

As an experienced website operation expert, I am well aware of the importance of the visual presentation of website content in terms of user experience and information delivery.In a powerful and flexible system like AnQiCMS, we have multiple ways to achieve fine-grained content control, especially in key navigation areas like category lists. How to effectively display category thumbnails or Banner images is an important aspect to enhance the professionalism and attractiveness of a website.Today, let us delve into how to cleverly integrate these visual elements into the category list in Anqi CMS.--- ###

2025-11-06

How to get the category details information of a specified ID through the `categoryList` tag instead of the current page?

As an experienced website operations expert, I know how important it is to flexibly obtain and display data when managing content.Especially when building the AnQiCMS website, we often encounter situations where we need to obtain specific category information rather than the context of the current page.Today, let's delve deeply into how to cleverly use AnQiCMS template tags to accurately obtain the category details you want.How to accurately locate: How to get the category details of a specified ID instead of the current page?In AnQiCMS template development, `categoryList` and

2025-11-06

How to use the "offset,limit" mode in the `limit` parameter supported by `categoryList`, and what scenarios are applicable?

## Masterful Manipulation of Category List: Practical Use of AnQiCMS `categoryList`'s `offset,limit` Pattern As an experienced website operations expert, I am well aware of how important flexible data display capabilities are when building and optimizing website content.AnQiCMS with its concise and efficient features provides us with many powerful template tags, among which the `categoryList` tag is the core tool for managing website classification content. Today

2025-11-06

In multi-site management, how do you call the category data under other sites using `categoryList`?

As an experienced website operations expert, I am well aware that how to efficiently manage content and achieve data interconnectivity in a complex website ecosystem is the key to improving operational efficiency.AnQiCMS (AnQiCMS) with its powerful multi-site management capabilities, provides us with such possibilities.Today, let's delve into a very practical scenario in multi-site operations: how to elegantly call the classification data under other sites using the `categoryList` tag.### AnQi CMS Multi-Site Management: `categoryList`

2025-11-06

If the category has not set Logo or Thumb, what path will `categoryList` return, and how to set the default image?

In the daily operation of AnQiCMS, fine-grained content display is a key factor in improving user experience and the professionalism of the website.The importance of images as visual elements is self-evident.However, many operators may encounter the situation that the Logo or Thumb image of the category is not set when calling the category information using tags like `categoryList`, which may cause blank or broken images to be displayed on the front end.Today, let's delve deep into this issue and provide an elegant solution.

2025-11-06