How to display all documents under the current category and its subcategories in AnQiCMS template?

Calendar 👁️ 70

In AnQiCMS template design, flexibly displaying content is the key to website operation.When you need to display articles in a category page not only the articles under the current category but also all the articles of the subcategories, or present them in a more structured way, AnQiCMS provides powerful template tags to meet these needs.This article will introduce you to how to implement this feature in your AnQiCMS template.

Understanding the category and document mechanism of AnQiCMS

In AnQiCMS, content is usually organized under 'Document Model' and 'Categories'.Each document (or article/product, etc.) belongs to a specific category, and categories can have parent-child hierarchical relationships.This hierarchical structure provides great flexibility for the organization and presentation of content.When we call content in the template, we need to specify which category (and its subcategories) to retrieve, as well as how to sort and limit the number.

Core tags:archiveListwithcategoryList

The core of displaying the current category and its subcategory documents lies in two template tags:

  1. categoryList(Category list tag): Used to retrieve category information, including the subcategories of the current category. By traversing this list, we can obtain the IDs and names of all subcategories.
  2. archiveList(Document list label)Used to retrieve the document list. This tag is very powerful, allowing you to filter documents through various parameters, the most critical of which iscategoryId(Category ID) andchild(whether to include documents in subcategories).

Next, we will demonstrate the specific operations through two common scenarios.

Scenario one: directly display the current category and all its subcategories of documents.

This is the most direct requirement, that is, to list all documents that belong to the current category and all its subcategories (including grandchild categories, etc.) on a category page in one go.

To achieve this function, we mainly rely onarchiveListlabel'scategoryIdandchildparameters. When you are in a category page template (such as{模型table}/list.htmlUsed inarchiveListif omittedcategoryIdparameter,archiveListit will try to read the current page's category ID by default. AndchildThe parameter is set to default.trueIt means it will automatically include all subcategory documents.

Code example:

{# 假设我们正在一个分类列表页面的模板中,例如 articles/list.html #}

{# 首先,可以获取当前分类的详细信息,用于标题等展示,这不是必须的,但有助于页面上下文 #}
{% categoryDetail currentCategory with name="Id" %}
{% categoryDetail currentCategoryTitle with name="Title" %}

<h1>{{ currentCategoryTitle }} 及下属所有分类文档</h1>

<div class="document-list-container">
    {% archiveList archives with type="page" limit="10" order="id desc" %}
        {% for item in archives %}
        <div class="document-item">
            <a href="{{ item.Link }}">
                <h3>{{ item.Title }}</h3>
                {% if item.Thumb %}
                <img src="{{ item.Thumb }}" alt="{{ item.Title }}" class="document-thumb">
                {% endif %}
                <p class="document-description">{{ item.Description }}</p>
                <div class="document-meta">
                    <span>所属分类: {% categoryDetail categoryName with name="Title" id=item.CategoryId %}{{ categoryName }}</span>
                    <span>发布日期: {{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
                    <span>浏览量: {{ item.Views }}</span>
                </div>
            </a>
        </div>
        {% empty %}
        <p>当前分类及其子分类下没有任何文档。</p>
        {% endfor %}
    {% endarchiveList %}

    {# 如果需要分页,可以结合 pagination 标签 #}
    {% pagination pages with show="5" %}
    <div class="pagination-nav">
        {% if pages.PrevPage %}<a href="{{ pages.PrevPage.Link }}">上一页</a>{% endif %}
        {% for p in pages.Pages %}<a href="{{ p.Link }}" class="{% if p.IsCurrent %}active{% endif %}">{{ p.Name }}</a>{% endfor %}
        {% if pages.NextPage %}<a href="{{ pages.NextPage.Link }}">下一页</a>{% endif %}
    </div>
    {% endpagination %}
</div>

Code explanation:

  • {% categoryDetail currentCategory with name="Id" %}The code will implicitly get the ID of the current category page and assign its value tocurrentCategoryVariable (although this variable is not used directly, butarchiveListwill be automatically used when not specifiedcategoryIdas the category ID of the current page).
  • {% archiveList archives with type="page" limit="10" order="id desc" %}:
    • archives: Define a variable to store the list of retrieved documents.
    • type="page": This is a paginated list.
    • limit="10": Displays 10 articles per page.
    • order="id desc": Sorted by document ID in descending order, meaning the most recent ones are at the top.
    • : Because it was not specified.categoryId,archiveListIt will intelligently obtain the ID of the current category.
    • : Because it was not specified.childThe parameter, it will use the default value.trueIt will automatically include all subcategory documents.
  • {% for item in archives %}...{% empty %}...{% endfor %}: Loop through the document list, if the list is empty, then display.{% empty %}The content inside.
  • {% categoryDetail categoryName with name="Title" id=item.CategoryId %}: Inside the document loop, based on each article'sCategoryIdDynamically obtain the name of its category.
  • {% pagination pages with show="5" %}: Used to generate pagination navigation, allowing users to switch between different pages.

Scenario two: Display in layers: First, display the current category documents, and then display the subcategory documents and their layers in turn

If you want the page structure to be clearer, for example, first display the direct documents under the current main category, then list the subcategories, and then list the documents under each subcategory, you can use nested loops.

Code example:

`twig {# Assume we are in a category list page template, such as products/list.html #}

{# 1. Get the details of the current category #} {% categoryDetail currentCategoryDetail %}

{{ currentCategoryDetail.Title_in_English }}

{{ currentCategoryDetail.Description in English }}

Display direct documents under the current category (does not include documents under subcategories) #

documents under the {{ currentCategoryDetail.Title }}

<div class="

Related articles

How to filter and display a list of documents in AnQiCMS using custom parameters?

In AnQiCMS (AnQiCMS), when managing and displaying a large amount of content, we often need to go beyond simple categories and tags to achieve more refined content filtering.For example, a real estate website may need to filter listings by "type", "area", and "price range";An e-commerce website may need to filter products by color, size, and brand.AnQiCMS provides a powerful custom parameter feature, making it very intuitive and efficient to implement this advanced filtering.Below, we will gradually understand how to use AnQiCMS

2025-11-08

How to display a list of articles published by a specific author in AnQiCMS?

In AnQiCMS, efficiently managing and displaying content is the core of website operation.Sometimes, you may need to display all the articles published by a specific author, such as creating an author column, a team member introduction page, or a personal portfolio.AnQiCMS's powerful template tag system provides a flexible way to meet this requirement, allowing you to easily filter and display a list of content from specific authors.This article will introduce you to how to display a list of articles published by a specific author in AnQiCMS, helping you better organize your website content.##

2025-11-08

How can articles be sorted by views in the AnQiCMS template?

In AnQiCMS, content management is not limited to publishing and organizing, how to effectively display these contents, especially placing popular articles in prominent positions, is the key to improving user experience and website activity.This article will introduce in detail how to sort the article list by views in the AnQiCMS template, making hot content naturally stand out.

2025-11-08

How does AnQiCMS allow search results to be paginated on the page?

In AnQiCMS, implementing pagination for the website's search results is an important function for improving user experience and optimizing the website structure.AnQiCMS's powerful template tag system makes this process intuitive and efficient.By reasonably utilizing the `archiveList` and `pagination` core tags, you can easily build a functional search results page.

2025-11-08

How to get and display the detailed information of a single article in AnQiCMS?

Managing and displaying content in AnQiCMS is one of its core functions.It is crucial to understand how to accurately retrieve and display the detailed content of a specific article when you need to present it to visitors.AnQiCMS provides a直观 and powerful template tag system that allows you to flexibly control the layout and content of the article detail page. ### Overview of AnQiCMS Template System The AnQiCMS template uses syntax similar to the Django template engine.In the template file, you will encounter two main markers: - **Double braces

2025-11-08

How to call and display custom field content in the article detail page of AnQiCMS?

In AnQiCMS, managing and displaying content, custom fields play a crucial role, enabling our website to flexibly carry various unique information, not limited to common attributes such as titles and content.Whether it is to add detailed technical parameters for product detail pages or to supplement author biographies and external reference links for articles, custom fields can provide strong support.Understand how to call and display these custom fields on the article detail page, which will greatly enhance the richness and customization of the website content. AnQiCMS

2025-11-08

How to display the links and titles of the previous and next articles on the article detail page?

When a reader visits an article of interest, they often hope to browse related or continuous content seamlessly, rather than returning to the list page to search again.Provide navigation links to "Previous" and "Next" articles on the article detail page of the website, which is an effective way to enhance this reading experience and increase user stickiness.It can not only guide users to explore the website content in depth, but also optimize the internal link structure of the website to a certain extent, making it friendly to search engines.AnQiCMS (AnQiCMS) is a powerful content management system that fully considers the needs of content operation

2025-11-08

How does AnQiCMS implement intelligent recommendation and display of related articles?

In content operation, effectively recommending relevant articles to readers can not only significantly increase user stay time and page views, but also indirectly promote search engines to crawl and allocate weights to the website's content through internal link optimization.AnQi CMS is well-versed in this, providing a smart and flexible mechanism to help users easily implement the recommendation and display of related articles.How does AnQiCMS implement intelligent article recommendation?

2025-11-08