How to build and display a multi-level navigation menu in AnQiCMS template?

Calendar 👁️ 79

The website navigation is the first window for users to interact with the website, a clear and intuitive navigation system can greatly enhance the user experience and help visitors quickly find the information they need.AnQiCMS provides flexible and powerful navigation management functions, whether it is a simple single-level menu or a complex two-level, three-level, or even more hierarchical multi-level navigation, it can be easily realized.Next, we will learn in detail how to build and display multi-level navigation menus in AnQiCMS.

Backend settings: The foundation for building navigation

In AnQiCMS, the construction of multi-level navigation begins with careful configuration on the backend. You can define the menu structure of the website through the "Website Navigation Settings" function on the backend.

1. Navigation Category Management

Firstly, navigation is not limited to the main menu at the top of the website.AnQiCMS allows you to create multiple navigation categories, such as main navigation, footer navigation, sidebar navigation, etc.In the "Navigation Category Management", you can add different navigation categories according to your website layout needs.The system will have a default 'default navigation' category, and you can create new categories as needed, such as creating a category named 'footer navigation'.Each category has a uniquetypeIdThis ID will be used in the front-end template to specify which navigation list to call.

2. Navigation Link Settings

After confirming the navigation category, the next step is to add specific navigation links to these categories.In the "Navigation Link Settings", you can add navigation entries for each category and define their hierarchy.

  • Parent navigation:This is the key to implementing multi-level navigation. You can choose an existing first-level navigation as the 'parent navigation' for the current link, so that the current link will become a sub-menu item.AnQiCMS supports native navigation up to two levels (i.e., main menu and sub-menus), which can be easily implemented through this setting.
  • Display Name, Subtitle Name, Navigation Description:These fields allow you to provide rich information for navigation items, such as main title, subtitle, or brief description, which is very useful in designing complex navigation menus.
  • Link Type:AnQiCMS provides three flexible link types:
    • Built-in links:Including the home page, article model home page, product model home page, and other commonly used pages, it is convenient to directly link to these functional pages.
    • Category page links:This is an important option for building dynamic multi-level navigation. You can choose any category or single page created on the website as a navigation link.
    • External links:You are allowed to enter any URL, whether it is an internal page or an external website link.
  • Display order:You can set a numeric order for each navigation link, with smaller numbers appearing earlier, allowing precise control of the navigation item arrangement.

With these settings, you can flexibly build various levels of navigation menus according to your website structure and design needs.

Template call: display navigation on the front end

Once the background navigation is set up, the front-end template can call and display these navigation data through specific tags.AnQiCMS uses a syntax similar to Django template engine, which is very intuitive.

1. UsenavListNavigation tag call

The core navigation call tag isnavList. It will extract the corresponding navigation data based on your configuration in the background.

BasicnavListThe label usage is as follows, it will store the obtained navigation data intonavsthe variable, and then you canforloop through these data:

{% navList navs with typeId=1 %} {# 这里的 typeId=1 对应后台“默认导航”的类别ID,请根据实际情况修改 #}
    {# 导航菜单的HTML结构将在这里构建 #}
{% endnavList %}

2. Build a single-level navigation menu

If you only need to display a level 1 menu, the code will be more concise:

<nav>
    <ul>
        {% navList navs with typeId=1 %}
            {% for item in navs %}
                <li class="{% if item.IsCurrent %}active{% endif %}">
                    <a href="{{ item.Link }}">{{ item.Title }}</a>
                </li>
            {% endfor %}
        {% endnavList %}
    </ul>
</nav>

In the code above,item.LinkGet the navigation link address,item.TitleGet the navigation display name.item.IsCurrentis a very useful boolean value, it will return when the current page matches the navigation linktrueand you can use it to add to the currently active menu itemactiveclass to achieve highlighting effect.

3. Build secondary navigation menu

To implement secondary navigation, it is necessary to usenavListdata returned initem.NavListproperties. If a navigation item has a submenu, thenitem.NavListThis will be an array containing sub-menu items. You can nest another loop inside the parent menu item.forLoop to display the sub-menu:

<nav>
    <ul class="main-menu">
        {% navList navs with typeId=1 %}
            {% for item in navs %}
                <li class="{% if item.IsCurrent %}active{% endif %}">
                    <a href="{{ item.Link }}">{{ item.Title }}</a>
                    {% if item.NavList %} {# 判断是否有子菜单 #}
                        <ul class="sub-menu">
                            {% for subItem in item.NavList %}
                                <li class="{% if subItem.IsCurrent %}active{% endif %}">
                                    <a href="{{ subItem.Link }}">{{ subItem.Title }}</a>
                                </li>
                            {% endfor %}
                        </ul>
                    {% endif %}
                </li>
            {% endfor %}
        {% endnavList %}
    </ul>
</nav>

This code demonstrates a typical secondary navigation structure. Whenitem.NavListit exists, a list will be rendered containing all the sub-menu items.sub-menuof<ul>列表,其中包含所有的子菜单项。

4. CombinecategoryListImplement multi-level dynamic navigation (suitable for deeper levels)

AnQiCMS'navListTags natively support two-level navigation. But if your website has a deeper classification hierarchy, or you want the navigation to be fully dynamic according to the classification structure (not just manually configured links in the background), then you can combine them to usecategoryList.

categoryListThe tag can retrieve the classification data of a specified content model and can recursively display subcategories. By usingcategoryListinnavListthe sub-menu section, or use it independentlycategoryListYou can build a more flexible multi-level dynamic navigation.

Here is a way tonavListwithcategoryListCombine to implement the example of 'Level 1 navigation (background configuration) -> Level 2 navigation (background configuration) -> Level 3 category (dynamically acquired)'

<nav>
    <ul class="main-menu">
        {% navList navs with typeId=1 %}
            {% for item in navs %}
                <li class="{% if item.IsCurrent %}active{% endif %}">
                    <a href="{{ item.Link }}">{{ item.Title }}</a>
                    {% if item.NavList %}
                        <ul class="sub-menu">
                            {% for subItem in item.NavList %}
                                <li class="{% if subItem.IsCurrent %}active{% endif %}">
                                    <a href="{{ subItem.Link }}">{{ subItem.Title }}</a>
                                    {# 假设 subItem.PageId 对应一个分类ID,且我们希望在其下动态显示其子分类 #}
                                    {% if subItem.PageId > 0 %}
                                        {% categoryList categories with parentId=subItem.PageId %}
                                            {% if categories %}
                                                <ul class="third-level-menu">
                                                    {% for category in categories %}
                                                        <li><a href="{{ category.Link }}">{{ category.Title }}</a></li>
                                                    {% endfor %}
                                                </ul>
                                            {% endif %}
                                        {% endcategoryList %}
                                    {% endif %}
                                </li>
                            {% endfor %}
                        </ul>
                    {% endif %}
                </li>
            {% endfor %}
        {% endnavList %}
    </ul>
</nav>

In this example, we first usenavListRetrieved navigation from two-level backend configuration. In the second-level navigationsubItemloop, we checksubItem.PageId(if the second-level menu is associated with a category ID), then usecategoryListGet all the subcategories of this category, thereby dynamically generating the third-level menu.categoryListalso supportsparentIdparameters to get the subcategories under the specified parent level, andHasChildren

Related articles

How to dynamically display the website's homepage TDK information in AnQiCMS?

The TDK of a website, which stands for Title (title), Description (description), and Keywords (keywords), is a basic element of Search Engine Optimization (SEO), which directly affects the display effect and user click intention of the website in the Search Engine Results Page (SERP).For the "face" of the website - the homepage, accurate and attractive TDK information is crucial.In AnQiCMS (AnQiCMS), managing and dynamically displaying the TDK information of the website homepage is a straightforward and efficient task

2025-11-08

How to display the website's contact phone number and address on the AnQiCMS front-end page?

In website operation, clearly and conveniently displaying the website's contact phone number and address is a key step in establishing user trust and promoting communication and interaction.For friends using AnQiCMS, the system provides a very flexible and intuitive way to manage and display this important contact information.This article will provide a detailed introduction on how to display the website's contact phone number and address on the AnQiCMS front-end page. ### Step 1: Enter contact information in the AnQiCMS backend Firstly, we need to ensure that the contact information of the website has been correctly entered in the backend.This is the foundation for displaying this information on the front end

2025-11-08

How to display custom copyright information in AnQiCMS templates?

In AnQiCMS website operation, the website footer usually carries important information, among which the copyright statement is not only a legal requirement, but also an embodiment of brand professionalism.Clear and dynamically updated copyright information that can enhance the professional image of the website and provide basic protection for the content.In AnQiCMS, managing and displaying custom copyright information on the website is a very intuitive and flexible operation.Next, we will learn how to set and display your custom copyright information in the AnQiCMS template.--- ### One

2025-11-08

How to obtain and display the filing number of the AnQiCMS website?

For websites operating in mainland China, the filing number (ICP number) is an indispensable element.It is not only a requirement of laws and regulations, but also an important symbol to enhance the credibility of the website and gain the trust of users.AnQiCMS knows this point and therefore considered convenient management and display of the filing number at the very beginning of system design, allowing website operators to easily complete the configuration. Next, we will together learn how to obtain (configure) and display the filing number on the AnQiCMS website.

2025-11-08

How to use the breadcrumb navigation tag of AnQiCMS to display the current page path?

In website operation, a clear navigation path is crucial for user experience and search engine optimization (SEO).A Breadcrumb Navigation is an auxiliary tool that can significantly improve the usability of a website, providing a quick and convenient way for users to return to their previous page and clearly indicating their position on the current page.In AnQiCMS, implementing breadcrumb navigation is very simple and flexible.AnQiCMS provides us with a dedicated breadcrumb navigation tag `breadcrumb`, through it

2025-11-08

How to get the category list of a specified content model and display it in AnQiCMS?

In AnQiCMS, flexibly obtaining and displaying the category list of the specified content model is the key to building a clear website structure and a good user experience.Whether it is product display, article archive, or service introduction, the category list plays an important navigation role.AnQiCMS's powerful template tag system makes this process intuitive and efficient.

2025-11-08

How to display detailed information and description of the current category in AnQiCMS?

When operating the AnQiCMS website, we often need to dynamically display detailed information and descriptions of the current category on the category page, such as the title, introduction, large picture, and even custom fields.This not only helps users better understand the theme of the current page, but also effectively improves the page's SEO performance.The Anqi CMS provides very flexible and intuitive template tags, making this task easy and simple.### Get to know the `categoryDetail` tag

2025-11-08

How to display the list and links of all single pages in AnQiCMS template?

In AnQiCMS, wanting to display a list and links of all single pages in a website template is actually a very common requirement, such as you may want to place a 'site map' or 'quick link' area at the bottom of the website, or you may need to list all 'service items' single pages in the sidebar.AnQiCMS provides a simple and powerful template tag that allows you to easily achieve this.### Understanding Single Page and Its Role In AnQiCMS, a single page (Page) is usually used to publish content that is relatively fixed

2025-11-08