How to use the `categoryList` tag to build and display a multi-level, nested category navigation menu?

Calendar 👁️ 70

In website operation, a clear and easy-to-navigate menu structure is a core factor in improving user experience and search engine friendliness.For websites with a large amount of content or a variety of product lines, building a multi-level, nested category navigation menu is particularly important.AnQiCMS (AnQiCMS) provides powerful template tag features, among whichcategoryListThe label is the key to achieving this goal.

This article will delve into how to utilizecategoryListThe label, flexibly builds and displays your multi-level category navigation menu.

Get to knowcategoryListThe label and its basic usage

categoryListThe tag plays a role in the Anqi CMS template in obtaining the list of category data. It helps us retrieve category information under different content models such as articles, products, etc.

Its basic structure is as follows when in use:

{% categoryList categories with moduleId="1" parentId="0" %}
    {# 在这里循环输出分类数据 #}
{% endcategoryList %}

In this example:

  • categoriesIt is a variable name defined for the category list data obtained, and you can name it according to your habit.
  • moduleIdThe parameter is crucial, it specifies which content model category you want to retrieve. For example,"1"typically represents an article model,"2"It may represent a product model. In the AnQi CMS backend, each category belongs to a specific content model, so this parameter is required to ensure that you can get the correct type of content category.
  • parentIdThe parameter is used to specify the ID of the parent category. When you set it to"0"It means that you want to get all top-level categories. This is the starting point for building a multi-level menu.

categoryListThe tag is in{% categoryList %}and{% endcategoryList %}betweenforLoop to iterate over each category item obtained. Each category itemitemProvided rich field information, for example:

  • item.IdUnique ID of the category.
  • item.Title: The display name of the category.
  • item.Link: The URL link of the category page.
  • item.Description: The introduction or description of the category.
  • item.ParentId: The ID of the parent category, for the top-level category, this value is usually0.
  • item.HasChildrenA boolean value indicating whether the current category has subcategories. This field is the key to achieving multilevel nesting.
  • item.IsCurrentA boolean value indicating whether the current category is the category of the current page, which can be used for highlighting.
  • item.Spacer: If present, it will add a visual prefix to the subcategory (such as an indentation symbol).

By flexibly using these fields, we can design various complex navigation menus.

Build a two-level classification navigation menu

Firstly, we start building the navigation from the top-level categories of the website. Suppose we want to buildmoduleIdWith"1"a two-level menu for the article model.

<nav class="main-navigation">
    <ul class="top-level-menu">
        {% categoryList categories with moduleId="1" parentId="0" %}
            {% for item in categories %}
                <li class="menu-item {% if item.HasChildren %}has-submenu{% endif %} {% if item.IsCurrent %}active{% endif %}">
                    <a href="{{ item.Link }}">{{ item.Title }}</a>
                    {% if item.HasChildren %}
                        <ul class="sub-menu">
                            {% categoryList subCategories with parentId=item.Id %}
                                {% for subItem in subCategories %}
                                    <li class="menu-item {% if subItem.IsCurrent %}active{% endif %}">
                                        <a href="{{ subItem.Link }}">{{ subItem.Title }}</a>
                                    </li>
                                {% endfor %}
                            {% endcategoryList %}
                        </ul>
                    {% endif %}
                </li>
            {% endfor %}
        {% endcategoryList %}
    </ul>
</nav>

In this example, we first useparentId="0"Get all top-level categories. While traversing each top-level category, we useitem.HasChildrento determine if it has subcategories. If there are subcategories, we<li>call within the current tag againcategoryListtags, but this time we willparentIdis set toitem.IdThis represents the current top-level category ID, from which the subordinate subcategories can be obtained. This makes it easy to build a two-level navigation menu.

Implementing nested navigation with three layers and more.

Build a navigation with three or more levels, the core idea is to nest repeatedlycategoryListTags, each timeparentIdSet to the upper levelitem.Id. This process can be extended indefinitely according to your needs.

The following is an example of a three-level nested navigation implementation:

<nav class="main-navigation">
    <ul class="level-1-menu">
        {% categoryList categories with moduleId="1" parentId="0" %}
            {% for item in categories %}
                <li class="menu-item {% if item.HasChildren %}has-submenu{% endif %} {% if item.IsCurrent %}active{% endif %}">
                    <a href="{{ item.Link }}">{{ item.Title }}</a>
                    {% if item.HasChildren %}
                        <ul class="level-2-menu">
                            {% categoryList subCategories with parentId=item.Id %}
                                {% for inner1 in subCategories %}
                                    <li class="menu-item {% if inner1.HasChildren %}has-submenu{% endif %} {% if inner1.IsCurrent %}active{% endif %}">
                                        <a href="{{ inner1.Link }}">{{ inner1.Title }}</a>
                                        {% if inner1.HasChildren %}
                                            <ul class="level-3-menu">
                                                {% categoryList subCategories2 with parentId=inner1.Id %}
                                                    {% for inner2 in subCategories2 %}
                                                        <li class="menu-item {% if inner2.IsCurrent %}active{% endif %}">
                                                            <a href="{{ inner2.Link }}">{{ inner2.Title }}</a>
                                                        </li>
                                                    {% endfor %}
                                                {% endcategoryList %}
                                            </ul>
                                        {% endif %}
                                    </li>
                                {% endfor %}
                            {% endcategoryList %}
                        </ul>
                    {% endif %}
                </li>
            {% endfor %}
        {% endcategoryList %}
    </ul>
</nav>

In this example, we loop in the second levelinner1In it, we judge againinner1.HasChildren. If true, call againcategoryList, willparentIdis set toinner1.Idto get the third-level classificationinner2This pattern can be extended to build a deeper navigation structure.

Practical tips and suggestions

When building multi-level category navigation, in addition to the correct use of tags, there are some practical considerations:

  • Background category management is the foundationAll category hierarchies must be correctly set in the 'Content Management' -> 'Document Category' of the Anqi CMS backend.Make sure you have correctly selected the 'parent category' when adding a subcategory.Only when the background data structure is clear can the front-end tags be accurately called.
  • Style control is indispensable:categoryListThe tag is responsible for outputting HTML structure, while the visual effects of the navigation menu (such as hover drop-down, expand/collapse animation, highlight display, etc.) are entirely dependent on your CSS and JavaScript code.Reasonably designed menu style can significantly improve user experience.
  • Mark the current statusUtilizeitem.IsCurrentVariable, you can easily add a specific CSS class to the category currently being accessed by the user and all its parent categories (for exampleactive), thus highlighting the user's position visually.
  • Avoid over nesting: AlthoughcategoryListSupport deep nesting, but from the perspective of user experience, too deep navigation levels often reduce the usability of the website. It is usually recommended to control the depth of navigation to three or four levels within.
  • Flexible applicationlimitParameterIf you only want to display some categories at a certain level, you can uselimit="数量"Parameters are controlled.

By processingcategoryListA deep understanding and flexible application of tags, you will be able to build any complexity of user-friendly multi-level classification navigation menus for the Anqi CMS website, thereby better organizing content, guiding users, and optimizing search engine crawling.

Frequently Asked Questions (FAQ)

Q1: Why is my multi-level menu only displaying top-level categories and not the subcategories on the page?A1: First, check if the subcategories of the top-level category in your security CMS backend "Document Category" are set correctly, and make sure that the parent category of the subcategories points to the correct top-level category ID. Second, confirm whether you are using the loop of the top-level category in the template code.{% if item.HasChildren %}To judge and nest the second onecategoryListLabel, and the secondcategoryListofparentIdWhether the parameter is correctly pointing toitem.Id.

Q2: How to highlight the category of the current user in the navigation menu?A2:categoryListEach tag'sitemincludes oneIsCurrentThe boolean variable. Whenitem.IsCurrentWithtrueit indicates that the current category is the category corresponding to the page being visited by the user. You can<li>or<a>add a conditional judgment in the{% if item.IsCurrent %}active{% endif %}, and then style it with CSS.activeClass adds highlight style.

**Q3:moduleIdWhat is the role of the parameter? If I don't want

Related articles

How to configure and display multilingual content in AnQi CMS to serve users in different regions?

In today's global internet environment, enterprises and content operators often need to provide content to users from different countries and regions.AnQiCMS (AnQiCMS) understands this need and therefore provides flexible and powerful multilingual content configuration and display capabilities, helping users easily expand into international markets and directly reach audiences with different language preferences.### Overview of AnQi CMS Multilingual Capabilities AnQi CMS considers multilingual support as one of its core strengths, aiming to provide users with a complete global content publishing solution

2025-11-08

How to implement lazy loading of images in article content to improve page loading speed and user experience?

In today's fast-paced online world, website loading speed is crucial for user experience and search engine rankings.If your website content contains a large number of images, optimizing their loading method will be a key step in improving website performance.Image lazy loading (Lazy Load) is a highly efficient solution. Why Image Lazy Loading is So Important? While website images can enrich content and enhance visual appeal, they are also often the main reasons for slow page loading.When a page carries many high-resolution images

2025-11-08

How to call and display product custom parameters (such as price, stock) on the product detail page?

In website operation, especially for product display websites, product details are far more than just titles and descriptions.In order to better meet user needs, it is particularly important to display various personalized parameters such as product price, inventory, color, material, and so on.AnQiCMS (AnQiCMS) takes advantage of its flexible content model and powerful template tag features, allowing you to easily call and display these custom parameters on product detail pages.### Preparations: Define Product Custom Parameters To display custom parameters on the product detail page, you first need to define these parameters in the AnQi CMS backend

2025-11-08

How to use the `archiveList` tag to display the latest 10 article titles and summaries on the homepage?

In Anqi CMS, the homepage often plays a crucial role in displaying the latest content and attracting visitors' attention.If you want to clearly list the latest 10 article titles and summaries on the homepage, the `archiveList` tag is the powerful tool to achieve this goal.It is flexible and easy to use, helping you easily display dynamic content in the most prominent position on the website.We need to display the titles and summaries of the latest 10 articles on the homepage by using the `archiveList` tag's core parameters

2025-11-08

How to dynamically retrieve and display the title and link of the previous and next articles on the article detail page?

When browsing articles on a website, navigation links such as 'Previous' and 'Next' often appear at the bottom.This seemingly simple detail actually has a significant impact on user experience and the consistency of website content.It not only guides visitors to continue exploring more content, helps to increase the average page visit duration, but also optimizes the internal link structure of the website.For friends using AnQiCMS, implementing this feature is much simpler than imagined, as it comes with powerful template tags that make it easy to dynamically retrieve and display this navigation information.###

2025-11-08

How to set up independent custom templates for specific articles, categories, or single pages to achieve personalized content display?

In content operations, creating unique visual and functional experiences for different parts of the website is crucial for enhancing brand image, optimizing user experience, and accurately conveying information.AnQiCMS (AnQiCMS) fully understands this, providing flexible template customization capabilities, allowing us to set personalized display templates for specific articles, categories, even independent single pages, thus achieving differentiated content presentation.The core of this feature lies in its ability to allow us to摆脱网站全局模板的限制, to design and apply specific layouts or styles targetedly, in order to better match content themes or operational objectives. For example

2025-11-08

Does AnQi CMS support the rendering of content in Markdown format and can it correctly display mathematical formulas and flowcharts?

For Anqi CMS users, whether they can use Markdown format for content creation and smoothly display complex mathematical formulas and visualized flowcharts is a key consideration for enhancing content expression.It is pleasing to see that Anqi CMS indeed provides good support for these advanced content formats, and through a set of flexible configuration mechanisms, allows content creators to easily achieve this goal. ### Enable Markdown Editor First, content creators need to make simple settings in the Anqi CMS backend.Enter the "Global Settings" menu

2025-11-08

How to dynamically display a list of friend links in the footer or sidebar of a website?

In website operation, friendship links are one of the important ways to enhance website authority, obtain external traffic, and improve user experience.A system that is easy to manage and can dynamically display friend links will undoubtedly greatly improve operational efficiency.AnQi CMS is an efficient content management system that provides flexible friend link management features, allowing you to easily display these valuable external resources in the footer or sidebar of your website.Friend link management in AnQi CMS is very intuitive.You only need to log in to the backend management interface of AnQi CMS

2025-11-08