How to implement nested list display of multi-level categories in AnQiCMS templates?

Calendar 👁️ 67

The navigation structure of the website is the core of user experience, a clear and organized multi-level classification list can not only help users quickly find the content they need, but also effectively improve the website's SEO performance.In AnQiCMS, with its flexible template tag system, achieving intuitive and efficient nested list display of multi-level categories becomes straightforward.

Understand the AnQiCMS category structure

On the AnQiCMS backend management interface, you will find that the category function allows you to create categories with hierarchical relationships.This means you can set a main category (top-level category) and its subcategories under it for articles, products, or other content models, forming a tree structure.For example, a "product" model can have "electronic products" as a top-level category, with "mobile phones" and "computers" as secondary categories, and "mobile phones" can further have "Android mobile phones", "Apple mobile phones", and so on as third-level categories.This structure is passed in the templateparentIdThis parameter is used to associate and display.

Core:categoryListTag

The key to achieving multi-level nested list classification lies in the AnQiCMS provided.categoryListTemplate tag. This tag is used to retrieve and display category information and supports controlling the retrieval of child categories under a specified parent category by parameters. Combined with the template engine'sparentIdfunction.forLooping and conditional judgments, we can easily construct arbitrarily deep categorized nested lists.

While usingcategoryListWhen labeling, you need to pay attention to several important parameters:

  • moduleId: Specify the content model category you want to retrieve.For example, if your article category belongs to article model with ID 1, and product category belongs to product model with ID 2, you need to specify it here.
  • parentIdThis is the core of implementing multi-level classification.
    • When set to"0"At this time, it will retrieve all top-level categories.
    • When set to the ID of a specific category (for exampleparentId=item.IdIt will retrieve all subcategories under the category with this ID.
  • HasChildren: In each category object obtained in the loop, there is aHasChildrenThe attribute is a boolean (true/false) value used to determine if the current category has child categories.This is very useful for deciding whether to continue to the next level of nesting.

Step-by-step implementation of nested list display of multi-level categories

Below, we will demonstrate how to implement the nested display of three-level categories in AnQiCMS template.This logic can be extended to more levels, just repeat the same pattern.

First, assume we have a content model ID of1article model and have set multi-level categories for it.

  1. to get the top-level categories (first-level categories)

    We first useparentId="0"to get all top-level categories.

    {% categoryList categories with moduleId="1" parentId="0" %}
        {# 顶级分类列表开始 #}
        <ul>
            {% for item in categories %}
            <li>
                <a href="{{ item.Link }}">{{item.Title}}</a>
                {# ... 这里将嵌套获取子分类 ... #}
            </li>
            {% endfor %}
        </ul>
        {# 顶级分类列表结束 #}
    {% endcategoryList %}
    
  2. Nested loop subcategory (second level category)

    Within the loop of the top-level category, we judgeitem.HasChildrento determine whether we need to get the subcategory. Ifitem.HasChildrenWithtrue, then call againcategoryListlabel, and the currentitem.IdasparentIdEnter to get the second-level category.

    {% categoryList categories with moduleId="1" parentId="0" %}
        <ul>
            {% for item in categories %}
            <li>
                <a href="{{ item.Link }}">{{item.Title}}</a>
                {% if item.HasChildren %} {# 判断是否有子分类 #}
                <div>
                    {% categoryList subCategories with parentId=item.Id %} {# 获取二级分类 #}
                    <ul>
                        {% for inner1 in subCategories %}
                        <li>
                            <a href="{{ inner1.Link }}">{{inner1.Title}}</a>
                            {# ... 这里将嵌套获取三级分类 ... #}
                        </li>
                        {% endfor %}
                    </ul>
                    {% endcategoryList %}
                </div>
                {% endif %}
            </li>
            {% endfor %}
        </ul>
    {% endcategoryList %}
    
  3. Further nesting (third-level category and more)

    Continue the logic of the second step, we judge again inside the loop of the second-level categoryinner1.HasChildrenif it istrueCall againcategoryList, willinner1.IdasparentIdPass in to get the third-level classification. This pattern can be repeated indefinitely to meet any hierarchical depth required.

The code example for implementing a three-level nested list in a complete AnQiCMS template is as follows:

<nav class="main-navigation">
    {% categoryList categories with moduleId="1" parentId="0" %}
    {# 顶级分类(一级分类)列表 #}
    <ul>
        {% for item in categories %}
        <li class="category-level-1">
            <a href="{{ item.Link }}">{{item.Title}}</a>
            {% if item.HasChildren %} {# 如果当前分类有子分类 #}
            <div class="sub-menu-container">
                {% categoryList subCategories with parentId=item.Id %} {# 获取二级分类 #}
                {# 二级分类列表 #}
                <ul class="category-level-2">
                    {% for inner1 in subCategories %}
                    <li>
                        <a href="{{ inner1.Link }}">{{inner1.Title}}</a>
                        {% if inner1.HasChildren %} {# 如果二级分类有子分类 #}
                        <div class="sub-menu-container">
                            {% categoryList subCategories2 with parentId=inner1.Id %} {# 获取三级分类 #}
                            {# 三级分类列表 #}
                            <ul class="category-level-3">
                                {% for inner2 in subCategories2 %}
                                <li>
                                    <a href="{{ inner2.Link }}">{{inner2.Title}}</a>
                                </li>
                                {% endfor %}
                            </ul>
                            {% endcategoryList %}
                        </div>
                        {% endif %}
                    </li>
                    {% endfor %}
                </ul>
                {% endcategoryList %}
            </div>
            {% endif %}
        </li>
        {% endfor %}
    </ul>
    {% endcategoryList %}
</nav>

In the code above, we useditem/inner1/inner2Variables should be used to distinguish between categories of different levels. You can name them according to your actual situation.<ul>or<li>tagsclassProperties, in order to control styles through CSS, for example.category-level-1,category-level-2,category-level-3To distinguish between menu items of different levels.

Common Techniques and Precautions

  • Control Display Level: If you only want to display two levels of categories, just delete the part of obtaining the third-level category from the above code. Throughitem.HasChildrenThe judgment, you can accurately control whether each category displays its subcategory list.
  • Handling empty statesInforCan be used in the loop,{% empty %}Labels to handle the situation when the list is empty, for example, displaying prompts such as 'No categories' to optimize the user experience.
  • Current active status: AnQiCMS category object (such asitem/inner1) usually containsIsCurrentA property used to determine whether the current category is the one being accessed by the user. You can use{% if item.IsCurrent %}active{% endif %}This logic adds a special CSS style to the currently active category to highlight it.

By this structured method, AnQiCMS allows you to flexibly build user interfaces that are both in line with content logic and aesthetically pleasing, thereby enhancing the overall navigation experience and content discoverability of the website.

Related articles

How to filter and sort articles by category ID and recommendation attributes when displaying the article list in AnQiCMS?

How to effectively organize and present article lists in a content management system is a key factor that directly affects user experience and website information architecture.AnQiCMS provides a flexible and powerful template tag that allows us to refine and sort articles based on category ID and recommended attributes, thereby achieving accurate content placement and optimized display. ### Understanding the `archiveList` article list tag The display of AnQiCMS article lists depends on the `archiveList` template tag.It is like a universal content query tool

2025-11-07

How does AnQiCMS manage and display single-page content, such as 'About Us' or 'Contact Us'?

In website operation, we often need to create some fixed content, large information independent pages, such as "About Us", "Contact Us", "Terms of Service" or "Privacy Policy" and so on.These pages are usually called single-page content, they are an indispensable part of the website, used to provide visitors with core information, build trust, or guide operations.AnQiCMS provides an intuitive and flexible solution for managing and displaying this type of single-page content.

2025-11-07

How to correctly display Markdown formatted mathematical formulas and flowcharts in AnQiCMS?

For users who often need to publish technical articles, tutorials, or academic content, the Markdown editor is undoubtedly a tool that improves efficiency.Its concise syntax makes content creation intuitive and quick. AnQiCMS, as a modern content management system, naturally takes full consideration of the use scenarios of Markdown and provides strong support.However, when we need to insert complex mathematical formulas or draw intuitive flowcharts in Markdown-formatted articles, relying solely on the features of Markdown itself is not enough. At this point

2025-11-07

How to implement custom display templates for category pages in AnQiCMS?

AnQiCMS provides excellent flexibility in website content management, especially when dealing with category page displays. It is not limited to a single fixed layout but allows users to set personalized display templates for category pages based on different business needs and design concepts.This is undoubtedly a very practical feature for operators who hope to improve user experience, strengthen brand image, or optimize specific SEO strategies through differentiated content display.How is it specifically implemented to customize the display template of the category page in AnQiCMS?

2025-11-07

How does AnQiCMS automatically handle image thumbnails to optimize the display speed of website content?

In today's fast-paced online world, the speed of displaying website content, especially the speed of image loading, directly affects user experience, website bounce rate, and even search engine rankings.Large images are often the culprits that slow down website speed, but without them, the content can become dull and boring.How can one have the cake and eat it too, showing high-quality images while ensuring the website content loads quickly?

2025-11-07

How to configure AnQiCMS's pseudo-static rules to achieve personalized URL display?

## Optimize URL structure, create personalized website links: AnQiCMS Static Rule Configuration Guide In website operations, URL (Uniform Resource Locator) is not only the address of content, but also an important part of Search Engine Optimization (SEO) and User Experience (UX).A clear, meaningful, and easy-to-remember URL structure that can effectively improve a website's ranking in search engines, as well as allow visitors to understand the page content more intuitively.

2025-11-07

How to use breadcrumb navigation tags in AnQiCMS templates to enhance user experience?

In modern web design, user experience (UX) and search engine optimization (SEO) are the two cornerstones of success.A clear and intuitive navigation system can not only guide users to find the information they need easily, but also help search engines better understand the structure of the website.Among them, Breadcrumb Navigation is an effective tool to enhance these two aspects of performance.For AnQiCMS users, making good use of its powerful template tag features can easily integrate high-quality breadcrumb navigation into the website.

2025-11-07

How does AnQiCMS ensure that uploaded image resources are displayed correctly on the front end and protected by copyright?

## Security CMS: The Front-end Display and Copyright Protection of Image Resources High-quality image resources are not only the key to attracting users and enhancing the value of content in modern website operations, but also crucial for smooth display on the front-end and copyright protection behind it.AnQi CMS deeply understands this, from image upload to frontend display, to copyright protection, it provides a considerate and efficient solution set, allowing us to focus on the content itself without worrying about the technical details.

2025-11-07