How to build a multi-level menu and display it on the front end using the AnQiCMS navigation list tag (`navList`)?

Calendar 👁️ 80

The navigation system of the website is the core bridge for users to interact with content, especially for sites with rich content or deep hierarchy, a clear and efficient multi-level menu is crucial. AnQiCMS provides a powerful and flexible navigation list tag (navList), you can easily build and display a multifunctional multi-level menu.

Make full use of AnQiCMS' navigation function, we need to start from the background configuration, then to the rendering of the front-end template, and step by step build the menu structure we desire.

One, background navigation configuration: lay the foundation for multi-level menus

In the AnQiCMS management backend, navigation settings are the first step in building a multi-level menu. You can find the relevant options under 'Backend Settings' -> 'Navigation Settings'.

First, you can create different navigation categories, such as main navigation, footer navigation, or sidebar navigation, which allows you to define independent menu systems for different areas of the website.By default, the system will provide a category named "default navigation".

After selecting or creating a navigation category, you can add specific navigation links. Each navigation link can set the following key information:

  1. Display name and subtitle:This is the text displayed on the front-end menu item, you can set the main title and the auxiliary subtitle as needed.
  2. Parent navigation:This is the core of implementing multi-level menus. When you add a new navigation link, you can select its 'parent navigation'.If you select 'Top Navigation', it will become a top-level menu item.If you select an existing first-level menu item, the current link will become a second-level menu under it.AnQiCMS back-end currently supports the configuration of up to two-level navigation links, that is, the first-level menu and its first-level sub-menu.
  3. Link Type:AnQiCMS provides flexible link types, including "built-in links" (such as home page, article model home page), "category page links" (linked to specific articles or product categories, single pages), and "external links" (pointing to any URL). When you need to link the navigation item to a specific category or page, the corresponding ID (PageId) will be saved, which is very useful when dynamically loading content on the front end.
  4. Display order:By adjusting the display order, you can control the arrangement of menu items at the same level, with smaller numbers appearing earlier.

With these settings, you can create a hierarchical menu structure intuitively in the background, for example:

  • Home
  • Product Center
    • Product Category A
    • Product Category B
  • Solution
  • About Us

These backend configurations are completed, and then you need to display them on the website through front-end template tags.

Part two: Front-end template rendering: usingnavListTags to display the menu

In the AnQiCMS template file,navListThe tag is used to render the navigation list. It can read the menu data configured in the background and provide it to the template in a structured manner for iteration and display.

navListThe basic usage is as follows:

{% navList navs %}
    {# 菜单项的HTML结构将在这里循环输出 #}
{% endnavList %}

here,navsIs a custom variable name that will contain all the background configuration navigation data.navListThe tag must be paired, and the code block in the middle will iterate over each navigation item.

navsA variable is an array object, and each array element (which we usually callitemAll represent a navigation link. EachitemThe object contains the following common fields:

  • item.Title: Display name of the navigation item.
  • item.Link: Link address of the navigation item.
  • item.IsCurrent: Boolean value, if the current page matches the navigation itemtrue, it can be used to highlight the current active menu.
  • item.NavList:The key point!This is a nested array, if the currentitemThere is a submenu,item.NavListwhich will include these submenu items. The structure of each submenu item is consistent with the parent levelitemSame.

Based on these fields, we can build a basic two-level menu structure:

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

In the code above, we first use{% navList navs %}Retrieve top-level navigation data. Then, through{% for item in navs %}Loop through each first-level menu item. Inside each first-level menu item, we check{% if item.NavList %}Does a submenu exist. If it does, use a nested{% for subItem in item.NavList %}loop to render secondary menu items.item.IsCurrentproperty to help us add a menu item for the current page.activeClass, so that style highlighting can be performed.

Third, expand the depth of the multi-level menu: dynamically load content.

AlthoughnavListThe tag itself supports the configuration of two-level menus in the background, but by combining other content tags, we can realize a deeper level of menu display in the front-end visually, for example, dynamically listing the document list under the category of a second-level menu item, or further displaying the third-level category.

Here are some common multi-level menu expansion scenarios:

1. Display the document list of the category under the second-level navigation

Assuming you have configured a primary navigation "Product Center" in the background, under which there are two secondary navigations "Product Category A" and "Product Category B", and these secondary navigations are associated with specific category IDs (inner.PageId). You may want to see the latest product list under the category 'Product Category A' when the mouse hovers over it.

<ul>
    {% navList navList with typeId=1 %} {# 假设typeId=1是主导航 #}
    {%- for item in navList %}
    <li>
        <a href="{{ item.Link }}">{{item.Title}}</a>
        {%- if item.NavList %} {# 如果有一级子菜单 #}
        <ul class="nav-menu-child">
            {%- for inner in item.NavList %}
            <li>
                <a href="{{ inner.Link }}">{{inner.Title}}</a>
                {% if inner.PageId %} {# 检查子菜单项是否关联了分类/页面ID #}
                    {# 使用 archiveList 标签,根据 inner.PageId(分类ID)获取文档列表 #}
                    {% archiveList products with type="list" categoryId=inner.PageId limit="8" %}
                    {% if products %} {# 如果该分类下有产品 #}
                        <ul class="nav-menu-child-child"> {# 视觉上的三级菜单 #}
                            {% for product in products %}
                            <li><a href="{{product.Link}}">{{product.Title}}</a></li>
                            {% endfor %}
                        </ul>
                    {% endif %}
                    {% endarchiveList %}
                {% endif %}
            </li>
            {% endfor %}
        </ul>
        {% endif %}
    </li>
    {% endfor %}
    {% endnavList %}
</ul>

In this example, we are in the first-level submenu (innerinside, checkinner.PageIdWhether it exists (i.e., whether the navigation item is associated with a category or page). If it exists, we will usearchiveListLabel, based oninner.PageIdGet the latest document (product) list under this category, thus visually building a three-level menu effect.

2. Display the sub-categories under the second-level navigation.

Another common requirement is that the second-level navigation item itself is a parent category, and you want to expand it to display all its subcategories below.

<ul>
    {% navList navList with typeId=1 %}
    {%- for item in navList %}
    <li>
        <a href="{{ item.Link }}">{{item.Title}}</a>
        {%- if item.NavList %}
        <ul class="nav-menu-child">
            {%- for inner in item.NavList %}
            <li>
                <a href="{{ inner.Link }}">{{inner.Title}}</a>
                {% if inner.PageId > 0 %} {# 同样检查是否关联了分类ID #}
                    {# 使用 categoryList 标签,根据 inner.PageId 获取其下级分类 #}
                    {% categoryList categories with parentId=inner.PageId %}
                    {% if categories %} {# 如果存在下级分类 #}
                    <ul class="nav-menu-child-child"> {# 视觉上的三级菜单 #}
                        {% for subCategory in categories %}
                        <li>
                            <a href="{{ subCategory.Link }}">{{subCategory.Title}}</a>
                        </li>
                        {% endfor %}
                    </ul>
                    {% endif %}
                    {% endcategoryList %}
                {% endif %}
            </li>
            {% endfor %}
        </ul>
        {% endif %}
    </li>
    {% endfor %}
</ul>

This example is similar to the previous one, but it uses nesting within the second-level menu.categoryListTag to get and display all the subcategories of the second-level menu items as the parent category.

Summary

Through AnQiCMS'snavListLabel, combined with flexible backend navigation configuration, as well as in the front-end templateifJudgment,forloop andarchiveList/categoryListWith other content tags, you can easily build a powerful, hierarchical multi-level website navigation system. This also improves the organization of website content.

Related articles

How to use the AnQiCMS custom field feature to add additional display parameters to the product detail page?

In a content management system, the product detail page often needs to display more rich and specific product parameters than basic information to meet the personalized needs of different industries and products.AnQiCMS provides a powerful custom field feature, allowing us to easily add these additional display parameters to the product detail page without modifying the core code.The next step is to detail the three-step process of using AnQiCMS custom fields to inject more vitality into your product detail page.

2025-11-08

How to implement scheduled article publishing in AnQiCMS to ensure content is displayed to users as planned?

## Content publishing no longer chaotic: AnQiCMS's scheduling feature helps you easily plan your content schedule In the fast-paced digital content world, maintaining the rhythm of content publishing is crucial.Whether it is a small and medium-sized enterprise, a self-media operator, or a multi-site manager, everyone knows that presenting content to users according to a plan can effectively enhance brand image and increase user stickiness.However, manual publishing is not only time-consuming and labor-intensive, but it is also easy to disrupt the established plan due to various unexpected situations.AnQiCMS is well-versed in this, and its built-in scheduled publishing function is exactly designed to solve these pain points

2025-11-08

How to configure the recommended attributes of articles (such as headlines, sliders) in AnQiCMS and display them in different areas on the front end?

Manage website content in Anqi CMS and make important information stand out to users, which is crucial for enhancing user experience and content marketing effectiveness.Among them, the recommendation attribute configuration of the article and the front-end display are important functions to achieve this goal.By reasonable settings, you can easily display articles such as "headline", "slide" and the like in different positions on the website, attracting visitors' attention.### First part: Set recommendation properties for articles in the background Firstly, we need to log in to the Anqi CMS backend management interface to assign "recommendation properties" to specific articles

2025-11-08

How to call the website logo, filing number, and copyright information to display in the footer in AnQiCMS template?

In website operation, the footer usually carries important content such as brand logo, legal statements, filing information, and copyright statements.This information not only concerns the professional image of the website, but also reflects trustworthiness and compliance.For those who use AnQiCMS, flexibly calling this information in templates to achieve dynamic updates is a basic and practical skill.AnQiCMS is renowned for its concise and efficient architecture and the high performance brought by the Go language. It provides an intuitive template tag system, making it very easy to display website content.

2025-11-08

How to set the SEO title, keywords, and description for a single page in AnQiCMS, and customize its template?

Managing website content in AnQiCMS, a single page (or independent page) is a very practical feature, such as common pages like "About Us", "Contact Information", and so on.These pages' content is relatively fixed, but its SEO performance and display form are crucial for the overall image and user experience of the website.AnQiCMS is designed with SEO-friendliness in mind and provides flexible template customization capabilities, allowing us to easily set up unique SEO information and customize the display templates for each single page.

2025-11-08

How to use the `thumb` filter to get a thumbnail image in AnQiCMS?

In a content management system, image thumbnails play a crucial role.They can not only accelerate page loading speed and optimize user experience, but also maintain visual consistency in various layouts, especially in list pages, recommendation positions, and other scenarios, which is particularly important.AnQiCMS (AnQi Content Management System) provides us with a convenient way to manage and access these thumbnails, where the `thumb` filter is a powerful tool to achieve this function.

2025-11-08

How to apply the `thumb` filter to the image URL in the AnQiCMS template?

In website content operation, images play a crucial role, as they can not only attract users' attention but also effectively convey information.However, the original large image that has not been optimized often becomes a bottleneck in website loading speed, affecting user experience and even search engine rankings.AnQiCMS fully understands this and has provided a simple yet powerful tool for content operators to solve this problem - that is the **`thumb` filter`**.Then, how is the `thumb` filter applied to the image URL in the AnQiCMS template?

2025-11-08

How to configure AnQiCMS to automatically generate thumbnails for uploaded images?

In website operation, high-quality images can significantly improve user experience and content attractiveness.However, the original large image not only occupies valuable server space, but also slows down the website loading speed, affecting SEO performance.AnQiCMS as an efficient content management system fully considers this point, providing users with flexible and powerful automatic thumbnail generation for images.By configuring it reasonably, you can enable the system to intelligently process uploaded images, automatically generate thumbnails that meet the needs of your website, and achieve a balance in the visual presentation and performance optimization of the website content.

2025-11-08