How to implement multi-level category nesting display in Anqi CMS?

Calendar 👁️ 74

The organization of website content is crucial for user experience and information retrieval efficiency.A clear and logical classification system can help visitors quickly find the information they need, and also lay a good foundation for the website's SEO.In AnQi CMS, implementing nested display of multi-level categories is the key to building this efficient content architecture.It is not just a simple listing of categories, but also through the hierarchical relationship, making the content modular and organized, whether it is product display, article archive, or service introduction, it can be presented to users in a more intuitive way.

Understanding the classification mechanism of Anqi CMS

AnQi CMS provides a powerful and flexible classification management function.In the background, you can operate under "Content Management" module -> "Document Classification".Here, each category is closely associated with a content model (such as an article model or a product model), and the system supports you in creating multi-level subcategories for these categories.This means you can build a complete content tree from a large category to a small category, and then to an even more detailed subcategory.For example, a 'product' category can have 'electronic products', and further down are 'cell phones', 'computers', and so on, which is clear to users.

Core: utilizingcategoryListLabel implementation of nested display

The core of realizing multi-level classification nested display lies in making full use of the Anqi CMS template.categoryListThe tag is used to retrieve and display the category list. Its strength lies in its ability to flexibly control the hierarchical relationship of the categories obtained through parameters.

We usually start by obtaining the top-level categories of a website, which are categories without parent categories.categoryListIn the tags, by settingparentId="0"You can get the top-level categories under all model IDs. These top-level categories are often the starting points of the website navigation bar or the main categories on the homepage.

After obtaining the top-level category, the next step is to display their subcategories. Anqi CMS provides a category object with aIdProperty, this is the unique identifier of the category. When you want to get the subcategories of a category, you just need to take the category'sIdpass tocategoryListlabel'sparentIdParameters can be used. This way, every time we loop to a parent category, we can call it againcategoryListTag to get its direct subcategory, thus achieving a hierarchical nesting effect.

To make this hierarchical relationship clearer in the code and to handle different situations where there may or may not be subcategoriescategoryListEach category object returned by the tag also contains aHasChildrenProperty. This boolean value tells us whether the current category has subcategories. We can use it to decide whether to continue rendering the list of subcategories, for example, ifHasChildrenWithtrueIf, render the subcategory; otherwise, it may only display the current category itself.

Here we use a simple three-level category nesting example to demonstrate:

{% categoryList categories with moduleId="1" parentId="0" %}
{# 这段代码将获取模型ID为1的所有顶级分类 #}
<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> {# 显示二级分类的链接和标题 #}
                    {% if inner1.HasChildren %} {# 判断当前二级分类是否有子分类 #}
                    <div>
                        {% categoryList subCategories2 with parentId=inner1.Id %} {# 获取当前二级分类的子分类 #}
                        <ul>
                            {% 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 %}

In this code, we first usecategoryListWe have obtainedmoduleIdFor all top-level categories with 1 (parentId="0")。Next, within the loop of each top-level category, we check{% if item.HasChildren %}whether it has a subcategory. If it does, we call it again.categoryListagain, but this time we setparentIdthe current top-level category toId(parentId=item.IdThus, we can retrieve and render the second-level category. Similarly, for the second-level category, we can also judge and retrieve its third-level category.By this recursive or nested calling method, you can display any depth of classification hierarchy according to your actual needs.

Considerations in practical applications.

In actual operation, embedding the above code into your Anqi CMS template file is the first step.Generally, category lists are placed in the navigation area of the website, the sidebar, or the filtering area of the content list page.According to the template design convention of Anqi CMS, the template file of the category list page is usually{模型table}/list.htmlIf you need to customize a template for a specific category ID, you can also use{模型table}/list-{文档分类ID}.html.

The code provides a multi-level classification structure, but to make it beautiful and easy to use on the front-end, it also needs to combine CSS styles to define the indentation, background, font, and other visual effects of different levels, as well as possibly need JavaScript to implement some interactive effects, such as displaying the submenu on mouse hover, expanding/collapsing on click, etc.Remember, the template code only provides the skeleton, and the beautiful 'outerwear' needs to be designed by yourself.

In addition,

Related articles

How to get all category lists under a specified model in AnQi CMS?

AnQi CMS with its flexible content model design makes content management efficient and elastic.Whether it is an enterprise official website, a marketing website, or a personal blog, there may be a need to display the corresponding category list according to different content models (such as articles, products, or custom models).Understanding how to accurately retrieve these categories in the template is a key step in fully utilizing the powerful functions of AnQi CMS.In AnQi CMS, the content model is the foundation of organizing content.

2025-11-08

How to get and display category thumbnails or Banner carousel?

The attractiveness of visual content is crucial in website operation.A well-designed category thumbnail and an eye-catching banner carousel not only beautifies the website interface but also effectively guides users to browse, enhancing the overall professionalism and user experience of the website.AnQiCMS (AnQiCMS) offers powerful and flexible features that allow you to easily manage and display these important visual elements.This article will provide a detailed introduction on how to obtain and display category thumbnails or Banner carousel in Anqi CMS.

2025-11-08

How to display the name, description, content, and link of the category?

When building a website, category information is not only the skeleton of the content, but also the key to user navigation, search engine optimization (SEO), and improving user experience.AnQiCMS (AnQiCMS) with its flexible template engine allows you to easily display categories, descriptions, content, and links on the website frontend, providing visitors with a clear navigation path and rich information for search engines to better understand your website structure.This article will delve into how to flexibly call and display your website's category information through several core tags in the AnQiCMS template.

2025-11-08

How to retrieve and display the custom parameter fields of the document in Anqi CMS?

AnQi CMS is loved by content operators for its excellent flexibility and highly customizable nature.In daily website management, we often encounter scenarios where it is necessary to add exclusive attributes for different types of content (such as articles, products, events, etc.)At this time, the document custom parameter field function of Anqi CMS can fully display its strength, allowing us to expand the structure of content according to business needs, thus achieving more personalized and rich display effects.

2025-11-08

How to retrieve custom fields of a category on the category detail page?

When managing website content in AnQi CMS, we often need to make the category page not just a simple aggregation of document lists, but also able to carry more exclusive information to better guide users or display the characteristics of the category.It is particularly important to add and obtain custom fields for categorization at this time.By using these custom fields, you can make each category page unique, whether it's displaying a specific banner image, contact information, or the unique properties of the category.

2025-11-08

How to display the complete content and all fields of a single page?

In Anqi CMS, a single page is the basis for building static content pages such as 'About Us' and 'Contact Information'.Effectively displaying the complete content of these pages and all their fields is crucial for website customization and user experience.The Anqi CMS provides intuitive and powerful template tags, allowing you to easily display single-page data configured in the background on the website front-end. To display the complete content of a single page and all related fields, we mainly use the `pageDetail` core template tag.

2025-11-08

How to retrieve and display all single page lists in AnQi CMS?

Managing and displaying single pages in Anqi CMS is one of the basic functions for website content construction.A single page is usually used to create pages with relatively fixed structures and independent content, such as 'About Us', 'Contact Information', or 'Service Introduction', etc.If you wish to display a list of single pages in a specific area of the website, such as the footer navigation or sidebar, AnQi CMS provides a very intuitive and flexible way to achieve this.

2025-11-08

How to display the document list under the specified Tag label and implement pagination?

In AnQi CMS, the Tag (tag) function is an important tool for organizing and associating content, enhancing user experience and website SEO performance.When your website content becomes richer, how to effectively display the document list under a specified Tag and provide clear pagination navigation is crucial for the discoverability of content and the continuous browsing of users.This article will give you a detailed explanation of how to easily implement this feature by using several key template tags in AnqiCMS.

2025-11-08