How can I build and display a multi-level nested category navigation menu?

Calendar 👁️ 73

Build and display multi-level nested category navigation menus in Anqi CMS, which is an important link in website content organization and user experience optimization.A well-designed and hierarchical navigation not only helps visitors quickly find the information they need, but also provides clear website structure signals to search engines, thereby enhancing SEO effects.

AnQi CMS is an enterprise-level content management system developed in Go language, with the flexibility of the content model and the strong support of template tags, making the implementation of multi-level classification navigation very intuitive and efficient.Next, we will discuss how to step by step build such navigation.

Backstage preparation: Build your content skeleton

Before starting to make a beautiful navigation menu, we need to set up the skeleton of content categories in the Anqi CMS backend.This is like designing the load-bearing structure of a building, which is the foundation for all subsequent displays.

First, you need to enter the "Content Management" module under the "Document Classification" page.Here, you can create various categories to organize the content of your website.When creating a category, you need to select a "document model" for it (such as "article model" or "product model"), which determines the content types and fields that can be published under this category.

The key to building multi-level navigation is to set the 'parent category'. For example, if you want to create a navigation about 'electronic products', you can:

  1. Create a top-level category named 'Electronics' without selecting any parent category.
  2. Under "Electronics", create secondary categories such as "Smartphone", "Laptop", and set their "parent category" to "Electronics".
  3. If you need a deeper level, you can continue to create 'Android phone', 'Apple phone' and other three-level classifications under 'Smartphone', and set their 'parent classification' to 'Smartphone'.

Through this method, the background category management interface will clearly present a tree-like hierarchical structure, laying a solid foundation for the front-end navigation display.

Template implementation: Flexible display of multi-level menus

The AnQi CMS template system adopts a concise syntax similar to Django, through built-in tags, we can conveniently call back-end data and display it flexibly. The core of building multi-level classification navigation is to usecategoryList.

1. Call the top-level category

First, we can usecategoryListtags to get all top-level categories, which are usually used as your main navigation menu items.

{% categoryList categories with moduleId="1" parentId="0" %}
    <ul class="main-nav">
        {% for item in categories %}
            <li><a href="{{ item.Link }}">{{item.Title}}</a></li>
        {% endfor %}
    </ul>
{% endcategoryList %}

here,moduleId="1"Suppose you are calling the classification under the 'Article Model' (the specific ID needs to be determined according to the model ID set in your background),parentId="0"This means to get only the top-level categories without parent categories.item.LinkIt will output the access link of the category,item.TitleThen output the name of the category.

2. Implement multi-level nesting

The essence of multi-level navigation lies in 'nested'. AnQi CMS.categoryListLabel combinationforLoops and conditional judgments can easily achieve arbitrary depths of nesting. We can check in each loop iteration whether a category has subcategories (viaitem.HasChildrenfield), and if so, call it againcategoryListGet and display these subcategories.

Here is an example of a three-level category navigation code:

{% categoryList categories with moduleId="1" parentId="0" %}
    <ul class="main-nav">
        {% for item in categories %}
            <li class="nav-item {% if item.IsCurrent %}active{% endif %}">
                <a href="{{ item.Link }}">{{item.Title}}</a>

                {# 判断当前分类是否有子分类 #}
                {% if item.HasChildren %}
                    <ul class="sub-nav">
                        {# 获取当前分类的子分类,parentId设置为当前item的ID #}
                        {% categoryList subCategories with parentId=item.Id %}
                            {% for subItem in subCategories %}
                                <li class="sub-nav-item {% if subItem.IsCurrent %}active{% endif %}">
                                    <a href="{{ subItem.Link }}">{{subItem.Title}}</a>

                                    {# 如果子分类还有更深层级,可以继续嵌套 #}
                                    {% if subItem.HasChildren %}
                                        <ul class="sub-sub-nav">
                                            {% categoryList deepSubCategories with parentId=subItem.Id %}
                                                {% for deepSubItem in deepSubCategories %}
                                                    <li class="deep-sub-nav-item {% if deepSubItem.IsCurrent %}active{% endif %}">
                                                        <a href="{{ deepSubItem.Link }}">{{deepSubItem.Title}}</a>
                                                        {# 理论上可以继续嵌套,但通常不建议导航层级过深,以免影响用户体验 #}
                                                    </li>
                                                {% endfor %}
                                            {% endcategoryList %}
                                        </ul>
                                    {% endif %}
                                </li>
                            {% endfor %}
                        {% endcategoryList %}
                    </ul>
                {% endif %}
            </li>
        {% endfor %}
    </ul>
{% endcategoryList %}

In this code, we first obtain the top-level category, and then within the loop of each top-level category, we useitem.HasChildrento determine if there is a subcategory. If there is, we use it againcategoryListLabel, andparentIdThe parameter is set to the current top-level category ofitem.IdThis process allows you to obtain the sub-categories. This process can be nested according to your actual needs to support more hierarchical levels of categorization.

item.IsCurrentThis field is also very useful, it can help you determine whether the category currently being looped to is the category corresponding to the page being visited by the user or its ancestor category, so that you can add it in CSS.activea class name to highlight display.

3. Style beautification

Although the above code has constructed a multi-level navigation HTML structure, in order to visually present the level and beauty, it still needs to be配合CSS styles. You can design your website according to your own design, to.main-nav,.sub-nav,.deep-sub-navand their internalliandaAdd appropriate styles to the tags, such as usingdisplay: none;and JavaScript or CSS's:hoverpseudo-classes to achieve the interactive effect of dropdown menus.

Further optimization and consideration.

  • Highlight the current page:To enhance users

Related articles

How to display the 'Previous' and 'Next' article links and titles on the article detail page?

It is crucial to enhance user experience and content discoverability in website operation.When a reader is immersed in an article, providing navigation links to the "previous article" and "next article" can not only guide them to continue browsing the website's content but also effectively reduce the bounce rate and optimize the on-site SEO structure.For those of us using the AnQiCMS website to manage, implementing this feature is both simple and efficient.AnQiCMS provides a straightforward and powerful template tag system, making it easy to integrate 'Previous Article' and 'Next Article' links on article detail pages

2025-11-08

How to call and display the custom fields of an article on the article detail page?

In Anqi CMS, the content display of the article detail page is highly flexible, especially for fields customized through the content model.These custom fields can help us manage and display different types of information more precisely, such as product prices, authors' origins, and property layouts, etc.How can you accurately call and display the content of these custom fields on the article detail page?This article will introduce this process in detail. ### 1. Custom field creation and filling: laying the content foundation In AnQi CMS

2025-11-08

How to display a list of related articles based on the keywords or custom associations of an article?

In website content operation, providing users with a list of relevant articles can not only effectively extend the visitor's stay time on the website and reduce the bounce rate, but also through guiding users to discover more interesting content, optimize the internal traffic path, and have a positive impact on SEO performance.AnQiCMS (AnQiCMS) understands this need and provides flexible and powerful functions, allowing us to easily display lists of related articles based on keywords or custom associations.###

2025-11-08

What sorting and filtering options does AnQiCMS provide to control the display order of list content?

How to efficiently display and organize content in website operation directly affects the user experience and the efficiency of information transmission.For users who use AnQiCMS, the system provides various flexible sorting and filtering options to help us accurately control the display order of list content. Whether it is articles, products, or other custom content types, they can be presented to visitors in the most business-friendly way possible.The AnQi CMS has given the control of content display order to the user, which is mainly realized through a powerful template tag system and backend management settings.Through these features

2025-11-08

How to display the top-level category list of a specific content model on the homepage or sidebar?

In AnQi CMS, managing website content, we sometimes need to clearly display the top-level category list of a specific content model at key positions such as the homepage or sidebar.This not only effectively guides visitors to browse the website content and improve user experience, but is also an important aspect of website content architecture optimization and search engine friendliness.Using AnQiCMS's flexible template system and powerful tag features, it is direct and efficient to meet this requirement. ### Understanding the Core: Content Model and Classification The core of content management in Anqi CMS lies in the "content model".You can create different content models based on business requirements

2025-11-08

How to create and display single-page content such as “About Us” and “Contact Us”?

In website operation, single-page content like "About Us", "Contact Us", and the like are windows to showcase the corporate image, core values, or provide key information to visitors.They usually carry stable, independent and infrequent updated content, which is crucial for building trust and providing service access.AnQiCMS provides very convenient and powerful functions for managing and displaying these single-page applications, allowing you to easily create and customize them.### Part 1: Create Your Single Page Content Creating a single page in AnQiCMS is an intuitive and efficient process

2025-11-08

How to display all used Tag tags on the website or only display Tag tags related to the current article in AnQiCMS?

When building website content, tags play an important role. They can not only help you better organize information and improve the discoverability of content, but also optimize the user experience, making it easier for visitors to find the content they are interested in.For a content management system, flexibly displaying and managing these tags is one of its core values.AnQiCMS provides powerful functions in this aspect, allowing you to easily control the display of tags on your website according to your actual needs.### Add and manage Tag tags On AnQiCMS backend

2025-11-08

How to display a list of all related articles under a specific Tag tag?

In Anqi CMS, tags (Tag) are an important tool for organizing content, enhancing user experience, and optimizing search engine performance.It can link articles from different categories but with related themes, making it easier for visitors to find content they are interested in.When you need to display a list of all associated articles under a specific Tag label, AnQi CMS provides a concise and powerful template tag to help you achieve this goal.### Understanding the Core: `tagDataList` tag To display the article list under a specific Tag, we mainly use the built-in functions of Anqi CMS.

2025-11-08