How to construct a website navigation with multi-level submenus and associated content using the `navList` tag?

Calendar 👁️ 79

When building a website, a clear and easy-to-use navigation system is crucial, as it directly affects whether users can quickly find the information they need and also influences the search engine's understanding of the website structure. AnQiCMS provides powerfulnavListLabel, allowing us to flexibly build a navigation system containing multi-level sub-menus and closely associating navigation items with website content.

Next, we will delve into how to utilizenavListLabel, create a set of multi-level navigation that is both beautiful and practical for your website.

The basics of website navigation: start from the backend settings

Before starting the template level work, we need to lay the foundation for the navigation system in the AnQiCMS backend.The AnQiCMS backend provides a 'Navigation Settings' feature, where you can define the entire navigation structure of the website.

Firstly, there is "Navigation Category Management". This allows you to create navigation areas with different names, such as "Top Main Navigation", "Footer Navigation", "Sidebar Navigation", and so on.This way, you can customize a dedicated navigation menu for different locations according to the needs of the website layout, each menu can be accessed through atypeIdto identify.

Next is the "navigation link settings", this is the place where specific menu items are composed. Here, you can add navigation links and choose their type:

  • Built-in linkFor example, the home page of the website, the home page of the article model, the home page of the product model, etc., these are all commonly used entry points preset by the system.
  • Category/single page link: You can directly select the article categories, product categories, or independent single pages that have been created in the background as navigation targets.This is very critical as it provides a bridge for us to associate more content in the template later.
  • External linkIf you need to link to external resources or a custom internal URL, you can choose this type.

In the background, each navigation link can be set to have up to two levels of hierarchy, that is, a main navigation item can have a sub-navigation item.Each navigation item includes information such as "Display Name", "Subtitle Name", "Navigation Description", and "Link Type", and can be sorted by "Display Order".

UnderstandingnavListThe core mechanism of tags

Once the navigation structure is set up on the backend, we can use it in the template.navListTag to call this data.navListThe usage of tags is as follows:

{% navList navs %}
    {# 循环输出导航项 #}
{% endnavList %}

here,navsIt is the variable name we define for the navigation list, you can name it according to your own habits.navListThe label supports several important parameters:

  • typeId: Corresponds to the navigation category ID defined in the background "Navigation Category Management". By specifyingtypeId, you can accurately call different menu groups such as top navigation, footer navigation, etc. For example,{% navList navs with typeId=1 %}The navigation category with ID 1 will be called.
  • siteIdIf you manage multiple sites and want to call data from a specific site, you can do so by:siteIdParameter to achieve.

In{% for item in navs %}In the loop, eachitemObjects contain the following useful fields:

  • Title: The display name of the navigation item.
  • SubTitle: The subtitle name of the navigation item.
  • Description: The description of the navigation item.
  • Link: The link address of the navigation item.
  • IsCurrent: A boolean value indicating whether the current navigation item corresponds to the page the user is visiting, commonly used to set highlight styles.
  • NavList: If the current navigation item has child navigation, this field will contain an array whose structure is the same as the parent.itemThe same, convenient for us to nest loops to build multi-level menus.
  • PageId:This is a very important fieldWhen the navigation item links to the "category page link" or "single page link" of the background,PageIdIt will store the ID of the category or single page. It is the key to achieving deep association between navigation and content.

Build a navigation with multi-level sub-menus.

AnQiCMS backend directly supports the configuration of two-level menus. In the template, we can achieve this two-level menu display through nested loopsitem.NavListto easily implement this two-level menu display:

<ul>
    {% 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 %} {# 判断是否有子菜单 #}
            <dl>
                {%- for inner in item.NavList %}
                    <dd class="{% if inner.IsCurrent %}active{% endif %}">
                        <a href="{{ inner.Link }}">{{inner.Title}}</a>
                    </dd>
                {% endfor %}
            </dl>
            {% endif %}
        </li>
    {% endfor %}
    {% endnavList %}
</ul>

This code first loops through the first-level navigation items, if a first-level navigation item is not empty,item.NavListit will continue to loop through its child navigation items, forming a standard two-level dropdown menu.

Linked content to achieve more flexible navigation

PageIdThe introduction of fields greatly expandsnavListThe function of tags. Although the background only directly supports two-level menus, but throughPageIdWe can dynamically call more related content based on the category or single page ID associated with the secondary menu item, thus achieving a complex effect of multi-level menus visually, or directly displaying the content list in the menu dropdown area.

Scenario one: Display more subcategories under the second-level menu (simulating a third-level menu)

Assuming your secondary navigation item is associated with an article category, and this category has more subcategories. You can useitem.PageIdCombinecategoryListThe label, dynamically pulling and displaying these third-level categories in the template.

<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 %} {# 检查二级菜单项是否关联了分类 #}
                    {% categoryList categories with parentId=inner.PageId %} {# 调用该分类的子分类 #}
                    {% if categories %}
                    <ul> {# 三级分类容器 #}
                        {% for subCategory in categories %}
                        <li>
                            <a href="{{ subCategory.Link }}">{{subCategory.Title}}</a>
                        </li>
                        {% endfor %}
                    </ul>
                    {% endif %}
                    {% endcategoryList %}
                {% endif %}
            </li>
            {% endfor %}
        </ul>
        {% endif %}
    </li>
    {% endfor %}
    {% endnavList %}
</ul>

In this code block,inner.PageIdascategoryListofparentIdThe parameter precisely specifies which subcategory of the category to retrieve. This way, even if the backend has not directly configured the third-level navigation, we can dynamically present it through the template logic.

Scenario two: Directly associate and display document/product list under the second-level menu

You can also choose to display the latest articles or hot products directly under the associated category in the dropdown area of the secondary menu item, rather than just displaying the subcategories.This is very practical for content display websites, such as e-commerce or information portals.

<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 %} {# 检查二级菜单项是否关联了分类 #}
                    {% archiveList products with type="list" categoryId=inner.PageId limit="8" %} {# 调用该分类下的最新8篇文档/产品 #}
                    {% if products %}
                    <ul class="nav-menu-child-content"> {# 内容列表容器 #}
                        {% for productItem in products %}
                        <li><a href="{{productItem.Link}}">{{productItem.Title}}</a></li>
                        {% endfor %}
                    </ul>
                    {% endif %}
                    {% endarchiveList %}
                {% endif %}
            </li>
            {% endfor %}
        </ul>
        {% endif %}
    </li>
    {% endfor %}
    {% endnavList %}
</ul>

We use here,inner.PageIdasarchiveListofcategoryIdParameters, retrieved document or product list associated with the second-level menu item category from the background. Bylimit="8"Can control the number of displays,type="list"then

Related articles

How to dynamically generate page title, keywords, and description with the `tdk` tag and flexibly control whether to include the website name?

In website operation, the page title (Title), keywords (Keywords), and description (Description), which we commonly refer to as TDK, are the foundation of search engine optimization.They can not only help search engines understand the page content, but are also key factors to attract users to click.AnQiCMS (AnQiCMS) fully understands the importance of TDK, and through its powerful template tag system, especially the versatile `tdk` tag, makes dynamic generation and flexible control of page TDK easy and efficient.

2025-11-07

How to use the `stampToDate` tag to format a timestamp into a custom date and time format?

Good, I'm glad to deeply interpret the `stampToDate` tag of AnQiCMS, helping you easily control the time display on the website. --- ## Flexible Time Mastery: Customize Date and Time Format with AnQiCMS `stampToDate` Tag The way dates and times are displayed on a website often directly affects user experience and the clarity of information communication in content management.A concise and readable date format allows visitors to quickly understand the timeliness of the content. AnQiCMS

2025-11-07

How to correctly configure the `pagination` tag to generate a functional pagination navigation on the list page?

In website content management, configuring a fully functional pagination navigation for the list page is a key factor in improving user experience and optimizing the website structure.AnqiCMS (AnqiCMS) provides a simple and powerful `pagination` tag, allowing developers to easily meet this need.To make the `pagination` tag generate the pagination navigation correctly on the list page, it is first necessary to understand its mechanism and the collaborative relationship with content list tags (such as `archiveList`).

2025-11-07

How to get and paginate the display of all relevant documents under a specific `tagDataList` tag?

When managing content in Anqi CMS, tags (Tag) are an important tool for organizing and categorizing information.It can not only help users quickly find relevant content, but also significantly improve the internal link structure and SEO performance of the website.When it is necessary to display all relevant documents under a specific tag and to manage pagination, the `tagDataList` tag becomes an indispensable core function.

2025-11-07

How to create a dynamic breadcrumb navigation using the `breadcrumb` tag and customize the home page name and title display?

In website operation, a clear navigation path is crucial for user experience and search engine optimization (SEO).Breadcrumb Navigation is like a small map of a website, allowing visitors to always know their location and easily return to the previous page.Today, let's delve deeper into how to use the powerful `breadcrumb` tag in Anqi CMS to create dynamic breadcrumb navigation and customize the home page name and title display according to our needs.

2025-11-07

How to perform content filtering, batch replacement, and recycle bin operations in the document management feature of the AnQiCMS backend?

The content management system is the core of website operation, especially for sites that update content frequently, efficient document management functions are crucial.AnQiCMS as a powerful content management system, provides a rich and practical background document management tool, helping operators easily handle daily content maintenance work.This article will delve into the functionality of AnQiCMS backend in content filtering, batch replacement, and recycle bin operations, helping you manage website content more efficiently.### Efficient Filtering: Quickly Locate Target Content Quickly locate the required information in the vast amount of website content

2025-11-07

How to create and manage custom content models in AnQiCMS and add specific fields to them?

In daily website operations, we often encounter the need to publish various types of content.In addition to common articles and product displays, you may also need a dedicated "event list" to post the latest events, or a "team members" page to showcase the core team of the company, or even a "successful case" module to share project experience.AnQiCMS was designed with this flexibility in mind, providing powerful custom content model features, allowing us to flexibly create and manage different types of content structures according to our actual business needs. So

2025-11-07

How to implement the image classification, batch transfer, and quick replacement operations of the image resource management function?

## Make image management no longer a hassle: Anqi CMS classification, batch transfer, and quick replacement feature details In this visually content-driven era, images on websites are not only auxiliary to content but also key elements that attract users, enhance experience, and even affect search engine rankings.However, as the content of the website becomes richer, the number of images also increases, and if these images are just scattered together, it will be extremely difficult to search and manage, and even slow down the efficiency of content updates.AanQi CMS understands the importance of image management for website operation

2025-11-07