How to display the second-level or multi-level category list in the navigation bar?

Calendar 👁️ 89

The website navigation is like the skeleton of your website, guiding visitors to browse different parts of the website and find the content they are interested in.A clear and intuitive navigation system is crucial for improving user experience and the professionalism of the website.AnQi CMS understands this and provides flexible and diverse solutions to build and manage website navigation, especially for complex navigation structures that need to display two-level or even multi-level classification lists.

Today, let's delve into how to set up and display these well-structured category lists in Anqi CMS, making your website navigation more powerful and intelligent.

Security CMS Navigation System Overview

In Anqi CMS, the website's navigation management is concentrated in the "background settingsHere, you can create different types of navigation menus, such as main navigation, footer navigation, or sidebar navigation, etc.The system provides a default "default navigation" category that you can modify on its basis, or create a new navigation category as needed.

The type of navigation links is very flexible, you can choose:

  • Built-in link指向网站的首页、不同内容模型的首页(如文章首页、产品首页等)。
  • Category page link直接关联到您已创建的文章分类或产品分类。
  • External linkCan point to any URL within the site, even links to other websites outside.

For multi-level category lists, Anqi CMS mainly provides two implementation methods: one is to directly configure multi-level menus using built-in navigation functions, and the other is to dynamically generate multi-level lists based on category data, both with their own focus.

Method one: Use built-in navigation features to build a multi-level list

This method is suitable for navigation structures that are relatively fixed, or when you want to finely control the display content of each navigation item.The built-in navigation of Anqi CMS supports navigation links up to two levels, that is, the first-level main menu and the second-level submenu under it.

First step: Configure multi-level navigation in the background

  1. Enter navigation settings:Log in to the AnQi CMS backend, click the left menu's 'Backend Settings', and then select 'Website Navigation Settings'.
  2. Manage navigation categories: If you want to create a navigation area other than the 'default navigation' (such as 'footer navigation'), you can add it in the 'navigation category management'.For ordinary main navigation, just use 'default navigation'.
  3. Add a first-level navigation link:
    • Click the 'Add Navigation Link' button.
    • “Parent navigation” select “Top navigation”.
    • Enter the “Display Name” (for example, “Product Center”, “About Us”).
    • Select the 'Link Type', if you want this navigation item to point to a category, select 'Category Page Link' and then select the corresponding category.
    • Set the display order, the smaller the number, the closer to the front.
  4. Add a secondary navigation link:
    • Click "Add navigation link" again.
    • This 'parent navigation' cannot be selected, instead, choose the first-level navigation item you just created (such as 'Product Center').
    • Fill in the “Display Name”, select the “Link Type”, and associate it with the corresponding category or page.
    • Set the “Display Order”.

In this way, you can clearly manage the display text, link address, and hierarchy of each navigation item in the background.

Step two: Call and display in the front-end template.

Configure the background navigation first, then you need to call these data through thenavListtags in the front-end template. Usually, this part of the code is placed inheader.htmlorbase.htmlsuch a public template file.

navListThe usage of tags is as follows:

{% navList navs with typeId=1 %} {# typeId=1 默认为主导航,如果自定义了类别请填写对应ID #}
<ul class="main-nav">
    {%- for item in navs %} {# 遍历一级导航项 #}
        <li class="{% if item.IsCurrent %}active{% endif %}">
            <a href="{{ item.Link }}">{{item.Title}}</a>
            {%- if item.NavList %} {# 判断当前一级导航项是否有二级子菜单 #}
            <dl class="sub-nav">
                {%- 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 %}
</ul>
{% endnavList %}

In the code above,navsThe variable will include all the first-level navigation items you configure in the background, eachitemrepresents a navigation item. Ifitem.NavListexists, it means that there is a second-level submenu under this navigation item, and you can use it againforLoop throughitem.NavListTo display the secondary menu content.item.IsCurrentThe attribute can help you determine whether the current navigation item is the current page, so that you can addactivea class name to highlight display.

Advanced usage: Display corresponding content under the second-level navigation

Sometimes, you may wish to expand the secondary navigation items not only to display the subcategory names but also to list the articles or product lists under the category directly.This is very practical in product displays or information websites.item.PageIdThe field provides the possibility to implement this function,item.PageIdIt will return the ID of the category or page associated with the current navigation item.

For example, in a product navigation, show 8 products under the second-level category:

<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>
                {% archiveList products with type="list" categoryId=inner.PageId limit="8" %} {# 使用 inner.PageId 获取产品列表 #}
                {% 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 %}
            </li>
            {% endfor %}
        </ul>
        {% endif %}
    </li>
    {% endfor %}
    {% endnavList %}
</ul>

Another common requirement is to display the third-level category under the second-level navigation, and so on. At this time, you caninnerUsing inside the loop againcategoryListTags:

<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 %} {# 使用 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>

Method two: Pure category-driven dynamic multi-level navigation

If your website's category structure is very large and often adjusted, or if you want the navigation to be generated completely based on the hierarchical relationship of the categories, then you should use directlycategoryListBuilding tags for navigation will be more efficient. This method can realize multi-level classification lists of any depth.

Step 1: Ensure that the classification structure is complete

Firstly, you need to make sure that the classification hierarchy is set up in the "Content Management" -> "Document Classification" of the Anqi CMS backend.For example, there are second-level categories under the first-level category, and third-level categories under the second-level category, and so on.

Step 2: Dynamically call in the front-end template

By recursive call or nestingcategoryListTags, you can easily build multi-level category navigation.

Here is an example code for a three-level category navigation.

”`twig {% categoryList topCategories with moduleId=“1” parentId=“0” %} {# Retrieve the top-level categories of the article model #}

    {% for level1_item in topCategories %} {# 遍历一级分类 #}
    <li>
        <a href="{{ level1_item.Link }}">{{level1_item.
    

Related articles

How to use the `archiveList` tag to sort and display the most popular article list by views?

How to display the popular article list by using the `archiveList` tag in AnQi CMS?In content operations, clearly displaying popular articles on the website to visitors can effectively improve user experience, guide users to discover more exciting content, and indirectly promote search engine optimization (SEO) through effective content distribution.AnQi CMS provides a powerful and flexible template tag feature, where the `archiveList` tag is the core tool to achieve this goal.

2025-11-07

How to get and display the thumbnail or cover image of the article in the template?

The Anqi CMS has always been highly praised for its flexibility in content display, where images act as the core elements to attract users and convey information. How to efficiently and accurately obtain and display the thumbnail or cover image of the article in the template is a focus for many website operators.This article will thoroughly explain the Anqi CMS template mechanism and guide you step by step to master the skills of image calls.How does AnQi CMS intelligently handle images

2025-11-07

How to customize the template layout of article, product, and category pages?

In AnQi CMS, flexibly customizing the page template layout is a key step to enhancing website personalization and user experience.Whether it is an article, product detail page, or category list page, AnQi CMS provides powerful and intuitive tools to allow you to create unique and stylish pages without delving into programming.Let's explore how to bring your design concept to life in Anqi CMS.

2025-11-07

Where is the TDK (Title, Keywords, Description) information of the home page set and displayed?

The TDK (Title, Keywords, Description) information on the homepage is crucial for the performance of the website in search engines.It is not only the key for search engines to understand the core content of a website, but also an important basis for users to decide whether to click on a website in the search results page.AnQi CMS is an efficient content management system that provides an intuitive and convenient TDK management function, allowing us to easily optimize the visibility of the website homepage in search engines.

2025-11-07

How to correctly render Markdown format in HTML and display mathematical formulas in the article content?

When using AnQiCMS to write articles, we often hope that the content is not limited to plain text or simple rich text, but can support richer formats, especially the convenience brought by Markdown syntax, as well as the indispensable mathematical formulas in scientific and technical content.This not only improves the quality of the content, but also provides readers with a better reading experience.AnQiCMS knows the importance of content diversity, and therefore provides Markdown support in the system.But let these Markdown contents, especially complex mathematical formulas

2025-11-07

How to display links to the previous and next articles at the bottom of the article detail page?

In Anqi CMS, adding navigation links for "Previous Article" and "Next Article" at the bottom of the article detail page can not only optimize the user's browsing path on the website, enhance the user experience, but also effectively increase page transitions, and is of great benefit to the internal link structure and SEO performance of the website.AnQiCMS is a powerful template engine with a rich set of tags, making it intuitive and efficient to implement this feature.### Core Function Analysis: Tags of the previous and next articles AnQiCMS template system provides a special function for retrieving tags of adjacent articles, respectively

2025-11-07

How to display the website's ICP record number and copyright information in AnQiCMS template?

In the operation of a website, the ICP filing number and copyright information are indispensable components for the legal operation and protection of the website's rights and interests.For websites operating in mainland China, the ICP record number is an explicit legal requirement. It not only guarantees the legality of the website but also enhances users' trust in the website.Copyright information specifies the ownership of the website content, helping to protect original works from infringement.

2025-11-07

How to embed dynamic contact information on the page, such as phone and address?

It is crucial to maintain the accuracy and consistency of contact information in website operation.This information, such as phone numbers, addresses, or social media links, often needs to be updated. Manually modifying each page not only takes time and effort but is also prone to errors.AnQiCMS (AnQiCMS) provides a very convenient way for you to dynamically manage and embed this contact information, achieving 'one-time modification, full-site update'. We will discuss in detail how to achieve this goal in Anqi CMS.### Centralized management of contact information on the backend First

2025-11-07