how the `categoryList` tag implements nested hierarchical display of multi-level categories?

Calendar 👁️ 54

Good, as an experienced website operations expert, I am happy to delve deeply into the Anqi CMS for youcategoryListHow to implement nested classification display of tags and transform it into an article that is easy to understand and practical.


Security CMScategoryListLabel: Easily implement nested classification display to make the website structure clear and organized

In website operation, a clear classification system is crucial for user experience (UX) and search engine optimization (SEO).A well-structured website can help users quickly find the information they need, while also allowing search engines to better understand the hierarchy of the website's content, thereby improving rankings.categoryListLabel, allowing us to skillfully build complex and layered website classification structures.

Today, let's delve into how to make use ofcategoryListTag, elegantly implement nested display of multi-level classification.

1. UnderstandingcategoryListThe core function of the tag

First, let's reviewcategoryListThe basic role of tags. In Anqi CMS template design,categoryListThe tag is mainly used to obtain the category list under the specified content model (such as articles, products, etc.). Its basic usage form is usually like this:

{% categoryList categories with moduleId="1" parentId="0" %}
    <!-- 在这里遍历分类数据 -->
{% endcategoryList %}

There are two very critical parameters here:

  • moduleId: Used to specify which content model category to retrieve. For example,"1"may represent the article model,"2"represents the product model. You need to determine the correct ID according to your backend settings.
  • parentIdThis is the core to implement multi-level nested classification.
    • WhenparentId="0"At that time, it will get all top-level categories, i.e., categories without any parent categories.
    • WhenparentIdWhen set to a specific category ID, it will retrieve all direct subcategories under that ID.
    • You can set it when you want to get the sibling categories of the current category in the category list page.parentId="parent".

BycategoryListLabel data obtained from classification, which is usually returned as an array object namedcategories(or a variable name you define)forLoop to traverse this array and access the details of each category, such asitem.Id(Category ID),item.Title(Category name),item.Link(category links) etc. Among them,item.HasChildrenThis field is particularly important, as it helps us determine whether the current category has a subcategory, thereby deciding whether to continue nesting display.

Level two: Implementing multi-level category nesting: step-by-step template construction

To implement nested display of multi-level categories, we usually adopt the strategy of 'outer loop to get parent level, inner loop to get child level'. Below, we will illustrate with an example of a three-level category:

1. Get the top-level category

Firstly, we start from the top level to get all the first-level categories. This is achieved by settingparentId="0"to achieve:

{% categoryList topCategories with moduleId="1" parentId="0" %}
    <ul class="level-1-categories">
        {% for item in topCategories %}
            <li class="category-item-1">
                <a href="{{ item.Link }}">{{ item.Title }}</a>
                <!-- 这里将是嵌套二级分类的地方 -->
            </li>
        {% endfor %}
    </ul>
{% endcategoryList %}

In the code above,topCategoriesThe variable contains all the first-level categories, and we traverse them to generate a link for each category.

2. Nested retrieval of second-level categories

Following that, while traversing the first-level categories,forWithin the loop, we can use it againcategoryListtags to get the subcategories of the current top-level category. At this point,parentIdthe parameter should be set to the current top-level category'sId, that isitem.Id:

{% categoryList topCategories with moduleId="1" parentId="0" %}
    <ul class="level-1-categories">
        {% for item in topCategories %}
            <li class="category-item-1">
                <a href="{{ item.Link }}">{{ item.Title }}</a>
                {% if item.HasChildren %} <!-- 判断当前分类是否有子分类 -->
                    <ul class="level-2-categories">
                        {% categoryList subCategories with parentId=item.Id %} <!-- 获取当前一级分类的子分类 -->
                            {% for inner1 in subCategories %}
                                <li class="category-item-2">
                                    <a href="{{ inner1.Link }}">{{ inner1.Title }}</a>
                                    <!-- 这里将是嵌套三级分类的地方 -->
                                </li>
                            {% endfor %}
                        {% endcategoryList %}
                    </ul>
                {% endif %}
            </li>
        {% endfor %}
    </ul>
{% endcategoryList %}

You can see, by calling the loop again in the parent categorycategoryListand the parent category'sIdpass toparentIdthen we have successfully retrieved and displayed the second-level category.{% if item.HasChildren %}The judgment makes our code more robust, avoiding the generation of empty items under items without subcategories.<ul>.

3. Continue to nest to get three levels and more.

As such, if you need to display a third-level category, simply repeat the above steps within the loop of the second-level category, and setparentIdto the current second-level category'sId(i.e.),inner1.Id):

`twig {% categoryList topCategories with moduleId=“1” parentId=“0” %}

<ul class="level-1-categories">
    {% for item in topCategories %}
        <li class="category-item-1">
            <a href="{{ item.Link }}">{{item.Title}}</a>
            {% if item.HasChildren %}
                <ul class="level-2-categories">
                    {% categoryList subCategories with parentId=item.Id %}
                        {% for inner1 in subCategories %}
                            <li class="category-item-2">
                                <a href="{{ inner1.Link }}">{{inner1.Title}}</a>
                                {% if inner1.Has

Related articles

How to get all top-level categories under a specified model for the `categoryList` tag?

Anqi CMS, as an efficient and customizable enterprise-level content management system, provides great flexibility in content organization and display.Among them, skillfully using template tags is the key to leveraging its potential.Today, let's delve into how to use the `categoryList` tag to accurately retrieve all top-level categories under a specified content model, which is crucial for building clear website navigation, content modules, or product display pages.

2025-11-06

How to create custom fields for different content models (such as articles, products) in AnQiCMS?

Good, as an experienced website operation expert, I am very willing to deeply analyze the powerful functions and actual operations of the customized fields in AnQiCMS. --- ## In AnQiCMS, create custom fields for different content models: unlock the powerful engine of personalized content In the era where content is king, the diversity and personalization of website content are key to attracting and retaining users.A remarkable content management system (CMS) should never limit you to a fixed content structure.AnQiCMS understands this way

2025-11-06

How does AnQiCMS handle the management of pseudo-static and 301 redirection to solve the problem of URL structure optimization and traffic loss?

## AnQiCMS' Static URL and 301 Redirect: A Smart Solution for URL Optimization and Traffic Protection In the digital wave, a website's URL (Uniform Resource Locator) is far more than a simple address.It is not only the path that users access the page, but also the key clue for search engines to understand the content of the website and evaluate its value.A clear and semantically friendly URL can significantly enhance user experience, improve search engine crawling efficiency and ranking performance.

2025-11-06

How to use AnQiCMS advanced SEO tools (such as Sitemap, Robots.txt) to improve the visibility of a website in search engines?

## Optimize Your Search Engine Footprint: How AnQiCMS Harnesses Sitemap and Robots.txt to Improve Website Visibility In the ever-changing digital world, the search engine visibility of a website is the foundation of its success.No matter if you are running a small and medium-sized enterprise, a self-media platform, or managing multiple sites, ensuring that your content can be discovered and effectively indexed by search engines is crucial for traffic acquisition and brand exposure.

2025-11-06

How to get the link, Logo image, and document count in the `categoryList` loop?

As an experienced website operations expert, I am well aware of the importance of an efficient and flexible content management system for daily operations.AnQiCMS with its high-performance architecture based on the Go language and the friendly syntax of the Django template engine, provides us with great convenience.Today, let's explore a very practical technique in website frontend display: how to elegantly obtain the links, Logo images, and the number of documents under each category in the `categoryList` loop.

2025-11-06

How `tagList` tags display the related Tag list of the specified document?

As an experienced website operations expert, I know that how to flexibly and effectively organize and display content in a content management system is the key to improving user experience and search engine optimization (SEO).AnQiCMS (AnQi CMS) offers many powerful functions in this aspect, and the `tagList` tag is an important bridge to connect content and enhance discoverability.Today, let's delve into how to use the `tagList` tag in AnQiCMS to accurately display the associated tag list of a specified document.### Fine-grained content management

2025-11-06

How to retrieve all documents under a specific Tag and implement pagination using `tagDataList` tag?

In the vast field of content operation, tags (Tags) play a crucial role.They can not only thematize and structure scattered content, but also guide users to explore deeply, improve the efficiency of content discovery, and enhance the overall user experience of the website.As an experienced website operations expert, I am well aware of the powerful capabilities of AnqiCMS (Anqi CMS) in label management and content display.

2025-11-06

How to use the `pageDetail` tag to get detailed content and images of a single page (such as "About Us")?

In website operation, pages like "About Us" and "Contact Us" are indispensable, as they carry important information such as corporate culture, contact information, and service introduction.How to present these carefully prepared contents efficiently and beautifully on the front end of a website is a skill that every website operator needs to master.For users using AnQiCMS, the `pageDetail` tag is the key tool to achieve this goal.

2025-11-06