How to create a multi-level category navigation and display its subcategories or related documents in AnQiCMS template?

Calendar 👁️ 61

Build flexible multi-level category navigation and content display in AnQiCMS template

Effective organization of website content is the key to providing a good user experience and improving search engine visibility.Whether it is a corporate website, product showcase, or self-media blog, a clear multi-level classification navigation can help users quickly find the information they need, and it is also conducive to search engines understanding the website structure.AnQiCMS as an efficient and customizable content management system provides flexible template tags, allowing us to easily implement complex multi-level classification navigation and content display.

Next, we will discuss how to create a multi-level category navigation in the AnQiCMS template and cleverly display its subcategories or related documents.

One, planning and setting of the background category structure

Before starting the template development, it is first necessary to ensure that the backend category structure is clear and logical.AnQiCMS allows you to create infinite-level categories and associate them with different content models (such as articles, products).

  1. Create content model:If you have not created one, you can define your content type under "Content Management" -> "Content Model", such as the "Article" model, "Product" model.Each model can have its own unique custom field.
  2. Build category hierarchy:
    • Enter under "Content Management" and "Document Categories" to start creating your top-level category.When adding a new category, select the corresponding "document model" and set the "parent category" to "top category".
    • Continue, create subcategories for these top-level categories. When adding subcategories, also select the corresponding "document model" and point the "parent category" to the top-level category you just created.You can repeat this process to build a multi-level classification structure.
    • In the "Other Parameters" category, you can set a custom URL, category template, SEO title, etc. for each category, which will provide more flexibility and optimization space for the front-end display.

Level two, implement multi-level category navigation in the template

AnQiCMS provides powerful template tags, allowing you to easily call and display the multi-level categories set in the backend on the front end. The most commonly used tag iscategoryListandnavList.

1. UsecategoryListBuild dynamic category navigation

categoryListThe tag is a core tool used to retrieve the list of articles or product categories. It can dynamically list categories according to your needs and supports multi-level nesting.

We usually combine to build a multi-level category navigationforloop andifto recursively display the categories.

Firstly, we can get all the top-level categories:

{% categoryList categories with moduleId="1" parentId="0" %}
<ul>
    {% for item in categories %}
    <li>
        <a href="{{ item.Link }}">{{ item.Title }}</a>
        {# 判断当前分类是否有子分类,如果有,则继续嵌套显示 #}
        {% if item.HasChildren %}
            {# 在这里再次调用 categoryList 获取当前分类的子分类 #}
            {% categoryList subCategories with parentId=item.Id %}
            <ul>
                {% for inner1 in subCategories %}
                <li>
                    <a href="{{ inner1.Link }}">{{ inner1.Title }}</a>
                    {# 可以继续嵌套第三级,甚至更多层级 #}
                    {% if inner1.HasChildren %}
                        {% categoryList subCategories2 with parentId=inner1.Id %}
                        <ul>
                            {% for inner2 in subCategories2 %}
                            <li>
                                <a href="{{ inner2.Link }}">{{ inner2.Title }}</a>
                            </li>
                            {% endfor %}
                        </ul>
                        {% endcategoryList %}
                    {% endif %}
                </li>
                {% endfor %}
            </ul>
            {% endcategoryList %}
        {% endif %}
    </li>
    {% endfor %}
</ul>
{% endcategoryList %}

In this code block:

  • The outermost{% categoryList categories with moduleId="1" parentId="0" %}We have obtainedmoduleIdAll top-level categories for 1 (assuming it is an article model).
  • item.Linkanditem.TitleUsed to output the link and name of the category.
  • {% if item.HasChildren %}Is a critical judgment, checking whether the current category has any subcategories.
  • If there is a subcategory, we will use it again internally{% categoryList subCategories with parentId=item.Id %}to categorize the current category,IdasparentIdPass in, so that you can retrieve and display its subcategories. This method can be nested infinitely to match the category depth set in the background.

2. UsenavListImplement multi-level navigation that is configurable in the background.

If you want the navigation structure to be flexible in the background "Navigation Settings" and not just automatically generated based on categories, thennavListtags are a better choice.navListSupport configuring two-level navigation in the background.

{% navList navs %}
<ul>
    {%- 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 %}
</ul>
{% endnavList %}

navListThe tag directly returns the preset multi-level navigation structure.item.NavListThe attribute is the sub-navigation list of it.item.IsCurrentIt can help you highlight the current navigation item on the page, enhancing the user experience.

3. Display subcategories or related documents in the navigation.

Whether it is usingcategoryListOrnavListBuild a multi-level navigation, and we can further obtain and display the documents under the sub-categories within its internal loop.

Assuming we want to display some articles 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>
                {# 这里我们假定inner.PageId是这个二级导航对应的分类ID #}
                {% if inner.PageId > 0 %}
                    {% archiveList products with type="list" categoryId=inner.PageId limit="8" %}
                    {% if products %}
                    <ul class="nav-menu-child-child">
                        {% for doc in products %}
                        <li><a href="{{doc.Link}}">{{doc.Title}}</a></li>
                        {% endfor %}
                    </ul>
                    {% endif %}
                    {% endarchiveList %}
                {% endif %}
            </li>
            {% endfor %}
        </ul>
        {% endif %}
    </li>
    {% endfor %}
    {% endnavList %}
</ul>

In the above example, we are in the loop of the second-level navigation:{% for inner in item.NavList %}Internally, through:archiveListThe label obtained the documents under the category associated with the current second-level navigation.

  • archiveListoftype="list"Indicates obtaining a normal list.
  • categoryId=inner.PageIdIt is crucial, it passes the current loop subcategory ID to the document list tag, thereby only displaying the documents under the subcategory.
  • limit="8"It then limits the number of documents displayed.

Four, Optimize User Experience and SEO

Building multi-level category navigation is not just about display, it also needs to consider user experience and search engine optimization:

  • Breadcrumbs navigation:On detail pages or list pages, throughbreadcrumbLabel displays the breadcrumb navigation at the current position, helping users understand their position in the website structure and facilitating navigation to the upper-level page.
    
    {% breadcrumb crumbs with index="首页" title=true %}
    <nav class="breadcrumb">
        {% for item in crumbs %}
            {% if not forloop.Last %}
            <a href="{{item.Link}}">{{item.Name}}</a> &gt;
            {% else %}
            <span>{{item.Name}}</span>
            {% endif %}
        {% endfor %}
    </nav>
    {% endbreadcrumb %}
    
  • Friendly URL structure:In the background "Feature Management" -> "Static Rules" select or customize SEO-friendly URL patterns, such as patterns containing category names or model names, which are very beneficial for search engine crawling and user memorization.
  • TDK (Title, Description, Keywords):Ensure each category page has a unique SEO title, keywords, and description. You can set this information separately for each category on the 'Document Category' editing page in the backend. In the template.

Related articles

How to filter the document list based on category ID, model ID, or recommendation attributes for the `archiveList` tag?

In AnQiCMS, flexibly displaying content is one of the keys to building an efficient website.The `archiveList` tag is such a powerful tool that allows us to filter and display the document list of the website based on various conditions.Whether you want to display a certain type of article on a specific page, or organize information based on content type or editor's recommendations, `archiveList` can provide precise control.### Accurate positioning: Filter document list by category ID When we want to display in a certain area of the website, such as the sidebar or a special topic page

2025-11-08

How to get the global configuration information of AnQiCMS template, such as the website name and logo?

In AnQiCMS template, cleverly obtain the global configuration of the website to make your website more flexible When designing or maintaining an AnQiCMS website template, we often encounter a need: to display unified global information on each page of the website, such as the name of the website, logo, filing number, or custom contact information.Manually adding this information to each page is not only inefficient but also time-consuming and laborious to update once it needs to be changed.

2025-11-08

How does AnQiCMS increase the level of content operation automation through the timed publishing function?

Today, with the increasing emphasis on efficiency and user experience, the degree of automation in content operation is directly related to the competitiveness of the website.For many small and medium-sized enterprises and self-media operators, how to ensure high-quality, frequent, and precise targeting of content under limited human resources is a continuous challenge.AnQiCMS is an efficient and flexible content management system, and its built-in timed publishing function exactly provides strong support for solving this problem.Imagine such a scenario: You have meticulously planned marketing content for a week or even a month, including articles, product updates, and event previews.

2025-11-08

How to implement multi-language content switching and display in AnQiCMS template?

In today's digital world, your website may need to be global for users.AnQiCMS fully understands this requirement and provides powerful multilingual support functions, allowing you to easily build and manage multilingual websites and better serve users from different countries and regions.How to implement multi-language content switching and display in AnQiCMS templates?Let's explore step by step. ### Understanding the multi-language mechanism of AnQiCMS: Handling two types of 'languages' In AnQiCMS, we need to distinguish between two types of 'language' content

2025-11-08

How to display comment content and user status for the `commentList` label in AnQiCMS?

Build a highly interactive website in AnQiCMS, the comment function is undoubtedly the key to increasing user engagement.The `commentList` tag is a great tool provided by AnQiCMS for this purpose, it can help website developers and operators flexibly display comment content, and clearly present the status of commenters, allowing visitors to grasp the dynamics of the comment area at a glance.### Core Feature Overview: Basic Usage of `commentList` Tag The `commentList` tag is mainly used to retrieve the comment list of a specified document

2025-11-08

How to enable and use the captcha feature of the留言表单in AnQiCMS?

The website guestbook is a good place to interact with visitors, which brings the website closer to the users.However, without proper protection, the message board will soon be overwhelmed by various spam, malicious submissions, and even automated scripts, which not only affects the cleanliness of the website but may also consume server resources and even damage the reputation of the website.Fortunately, AnQiCMS provides a simple and effective solution: the captcha feature.

2025-11-08

How to use the `archiveFilters` tag in AnQiCMS to achieve combined filtering of document parameters?

## Mastering AnQiCMS: The Art of Using `archiveFilters` Tag for Document Parameter Combination Filtering It is crucial to provide users with accurate and efficient content search experience in content operation.Imagine if your website has a rich variety of content, but users find it difficult to quickly find the information they need. This will undoubtedly greatly affect user experience and the conversion rate of the website.

2025-11-08

How to automatically generate the breadcrumb navigation of the page in the AnQiCMS template using the `breadcrumb` tag?

When browsing websites, we often notice a path information string at the top or bottom of the page, which clearly indicates our current location, such as "Home > Category > Article Title."}This is what we commonly refer to as Breadcrumb Navigation.It not only helps users better understand the website structure and improve the browsing experience, but also has a significant value for search engine optimization (SEO).Why is breadcrumb navigation important? For users, breadcrumb navigation is like a mini-map.

2025-11-08