How to use the `categoryList` tag to build a multi-level category navigation or display category lists in the content area?

Calendar 👁️ 75

In Anqi CMS, properly organizing website content categories is an important factor in improving user experience and website maintainability. Whether it is to build a clear multi-level navigation menu to help users quickly locate information, or to skillfully display different categories of content in the page content area,categoryListTags are the core tools for implementing these functions.

Core tags:categoryListIntroduction

categoryListThe tag is used in the Anqi CMS template engine to retrieve the list of website categories.It is very flexible and can retrieve the classification of specific content models, the sub-classifications under specific parent classifications, and even all classifications of the entire website according to your needs.

The basic structure of using it is like this:{% categoryList categories with moduleId="1" parentId="0" %} ... {% endcategoryList %}

Let's break down the key parameters of this tag:

  • moduleId: Anqi CMS supports multiple content models, such as you may have article models (usually ID 1), product models (usually ID 2), and so on.moduleIdThe parameter is used to specify which content model category you want to retrieve. For example,moduleId="1"It will retrieve article categories,moduleId="2"It will retrieve product categories.
  • parentId: This is the key to controlling the hierarchical relationship of categories.
    • If you want to gettop-level categoriescan be set toparentId="0".
    • If you are looping a category, and you want to get its directsubcategoriesthen you can omitparentIdparameter,categoryListThe tag reads the current loop category ID as the parent ID intelligently.
    • If you want to get the current category'ssibling categorycan be set toparentId="parent".
  • limit: If you only want to display a specific number of categories, you can uselimitParameters to limit the number of returned items, such aslimit="10". You can even setlimit="2,10"to indicate starting from the second category and retrieving 10 categories.
  • siteId: When you use the multi-site feature, you can go throughsiteIdSpecify the category data to be retrieved under a specific site. This parameter can usually be ignored.

WhencategoryListAfter the tag is executed, it will return a namedcategories(You can also customize this variable name) array. When you useforloop through this array, eachitemrepresents a category, which contains rich properties, such as:

  • item.IdUnique ID of the category.
  • item.Title: Category name.
  • item.LinkCategory link address, can be used directly<a>label'shrefProperty.
  • item.DescriptionCategory Introduction.
  • item.LogoCategory logo image address.
  • item.ThumbCategory thumbnail image address.
  • item.HasChildrenA boolean value used to determine whether the current category has subcategories, which is very useful when building a multi-level menu.
  • item.IsCurrentA boolean value indicating whether the current category is the category of the page being accessed by the user, often used for navigation highlighting.
  • item.ArchiveCountThe number of documents under the current category (excluding subcategories).

Build multi-level classification navigation: allow users to find the direction at a glance

A well-structured website navigation is like a map, which can help users find their destination quickly. UsecategoryListLabel, you can easily build a complex navigation structure from a top-level menu to multi-level dropdown menus.

We usually start by obtaining the top-level category, which serves as the first level of our navigation menu.

{# 假设我们正在构建一个文章分类的导航菜单,文章模型ID为1 #}
<ul class="main-nav">
    {% categoryList topCategories with moduleId="1" parentId="0" %}
    {% for item in topCategories %}
    <li class="nav-item {% if item.IsCurrent %}active{% endif %}">
        <a href="{{ item.Link }}">{{ item.Title }}</a>
        {# 检查当前分类是否有子分类,如果有,则渲染子菜单 #}
        {% if item.HasChildren %}
        <ul class="sub-nav">
            {# 再次调用 categoryList,此时 parentId 留空,它会自动获取当前 item.Id 的子分类 #}
            {% categoryList subCategories %}
            {% for subItem in subCategories %}
            <li class="sub-nav-item {% if subItem.IsCurrent %}active{% endif %}">
                <a href="{{ subItem.Link }}">{{ subItem.Title }}</a>
                {# 您可以继续在这里嵌套,以构建三级甚至更多级别的菜单 #}
                {% if subItem.HasChildren %}
                <ul class="sub-sub-nav">
                    {% categoryList subSubCategories %}
                    {% for subSubItem in subSubCategories %}
                    <li class="sub-sub-nav-item {% if subSubItem.IsCurrent %}active{% endif %}">
                        <a href="{{ subSubItem.Link }}">{{ subSubItem.Title }}</a>
                    </li>
                    {% endfor %}
                    {% endcategoryList %}
                </ul>
                {% endif %}
            </li>
            {% endfor %}
            {% endcategoryList %}
        </ul>
        {% endif %}
    </li>
    {% endfor %}
    {% endcategoryList %}
</ul>

This code demonstrates how to use nestingcategoryListtags to build multi-level navigation. The key is the internalcategoryListIt will be based on the external loopitem.IdAutomatically obtain its subcategory, anditem.HasChildrenIt is responsible for determining whether the submenu structure needs to be rendered. At the same time,item.IsCurrentIt can help you add a category for the current visit.activeClass, implement navigation highlighting effect.

Display the category list in the content area: enrich the page content.

categoryListLabels are not just used for navigation; they also play an important role in the main content area of a page, such as displaying the 'Hot Categories' on the homepage or a specific module's classification content block.

For example, on the homepage of the website, we may want to display the two main categories of "Articles" and "Products", and show several of the latest articles or products under each category. This requires thatcategoryListwitharchiveList(Document list tag) combined for use.

{# 假设文章模型ID为1,产品模型ID为2 #}
<div class="content-sections">
    {# 获取顶级文章分类 #}
    {% categoryList articleCategories with moduleId="1" parentId="0" limit="3" %}
    <section class="section-block">
        <h2>最新文章</h2>
        <div class="category-grid">
            {% for cat in articleCategories %}
            <div class="category-card">
                <h3><a href="{{ cat.Link }}">{{ cat.Title }}</a></h3>
                <p>{{ cat.Description|truncatechars:80 }}</p> {# 截取分类描述,限制字符数 #}
                <ul class="article-list">
                    {# 在每个分类下获取最新的3篇文章 #}
                    {% archiveList articles with categoryId=cat.Id limit="3" order="id desc" %}
                    {% for article in articles %}
                    <li><a href="{{ article.Link }}">{{ article.Title }}</a></li>
                    {% endfor %}
                    {% endarchiveList %}
                </ul>
            </div>
            {% endfor %}
        </div>
    </section>
    {% endcategoryList %}

    {# 获取顶级产品分类 #}
    {% categoryList productCategories with moduleId="2" parentId="0" limit="3" %}
    <section class="section-block">
        <h2>热门产品</h2>
        <div class="category-grid">
            {% for cat in productCategories %}
            <div class="category-card">
                <h3><a href="{{ cat.Link }}">{{ cat.Title }}</a></h3>
                <img src="{{ cat.Thumb }}" alt="{{ cat.Title }}" class="category-thumb">
                <ul class="product-list">
                    {# 在每个分类下获取最新的4个产品 #}
                    {% archiveList products with categoryId=cat.Id limit="4" order="id desc" %}
                    {% for product in products %}
                    <li><a href="{{ product.Link }}">{{ product.Title }}</a></li>
                    {% endfor %}
                    {% endarchiveList %}
                </ul>
            </div>
            {% endfor %}
        </div>
    </section>
    {% endcategoryList %}
</div>

This code demonstrates how to iterate over several top-level categories and then, within each category, to usearchiveListLabel to retrieve the specific articles or products under the category. This allows users to view rich and categorized content without leaving the current page.

Practical skills and precautions

  • moduleIdimportanceAlways remember to specify the correct content type (article, product, etc.) that you want to obtainmoduleIdto avoid getting incorrect categorized data.
  • limitthe flexible applicationWhether it is in the navigation or content area,limitParameters can help you control the number of displays to avoid the page from being too bulky.
  • item.IsCurrentHighlight implementationIn the navigation menu, make use ofitem.IsCurrentThis boolean value can easily add a CSS class to the category being accessed by the current user, highlighting it to enhance the user experience.
  • **Style with

Related articles

How do the `prevArchive` and `nextArchive` tags display the link and title of the previous/next document on the document detail page?

When we browse content on the website built with AnQiCMS, carefully crafted documents bring value to readers.It is particularly important to provide 'Previous' and 'Next' navigation links at the end of each article to make the reader's reading experience more smooth and encourage them to explore more relevant content.This can effectively guide users to deeply browse within the website, increase page visits and user stickiness, and is also very beneficial for the internal link structure and SEO optimization of the website.

2025-11-08

How to use the `archiveDetail` tag to retrieve and fully display the title, content, and custom fields of a specific document?

In AnQiCMS, displaying the detailed content of a single document is one of the core requirements for website construction.Whether it is an article detail page, a product detail page, or other custom type of content display, we need a flexible and efficient way to obtain and present the title, body of the document, and custom fields defined according to business requirements.The `archiveDetail` tag was born for this purpose, it can help you easily achieve this goal.### Deeply understand the `archiveDetail` tag

2025-11-08

How to accurately control the display quantity, sorting, and filtering conditions of the `archiveList` tag?

In Anqi CMS, the `archiveList` tag is a core tool for dynamically calling and displaying document lists.Whether it is a blog article, product showcase, or news update, this tag can help you flexibly control the display quantity, sorting method, and various filtering conditions according to your specific needs.A deep understanding of its parameters will enable you to build a highly customized and powerful content display page.

2025-11-08

How to customize independent display templates for specific content on the AnQiCMS website (such as documents, categories, single pages)?

In website operation, we often encounter scenarios where we need to give specific content a unique appearance.It may be a "About Us" page carrying important information, needing a unique layout; it may also be a series of product details, hoping to show different parameter comparisons; or even a content category, looking forward to presenting the list in a more attractive way.

2025-11-08

How to retrieve and display the name, description, Banner image, and thumbnail of a specific category using the `categoryDetail` tag?

In AnQiCMS template creation, we often need to retrieve and display detailed information about specific categories, such as the name, description, and even preset Banner images and thumbnails.`categoryDetail` tag is born for this, it helps us easily extract this data from the database and flexibly display it on the website front end.

2025-11-08

How to effectively manage and display single-page content on a website using `pageList` and `pageDetail` tags?

In website operation, single-page content plays an indispensable role, it usually carries important information such as "About Us", "Contact Us", "Terms of Service", "Privacy Policy", etc., which needs to be clearly displayed and also requires convenient management.AnQiCMS provides the powerful template tags `pageList` and `pageDetail`, which help us efficiently manage and display these single-page content.

2025-11-08

How to display all related document lists under a specific `tagDataList` tag and support pagination?

In AnQi CMS' daily content management, we often need to organize and display website content more flexibly.In addition to the traditional classification system, tags (Tag) provide us with another powerful way to associate content.It can aggregate documents from different categories but with similar themes, providing users with a more focused browsing experience.Today, let's delve into a very practical template tag in Anqi CMS, `tagDataList`, and see how it helps us display all related document lists under a specific tag and easily implement pagination.

2025-11-08

How to use the `navList` tag to create the main navigation menu of a website and support two-level dropdown display?

The main navigation menu of the website is an important entry for users to understand the content and structure of the website.The Anqi CMS provides a powerful and flexible `navList` tag, which helps us easily create and manage multi-level dropdown navigation menus, thus enhancing the user experience and clarity of the information architecture of the website. ### Step 1: Configure the navigation menu in the AnQi CMS backend Before starting to write template code, we need to set up the structure of the navigation menu in the AnQi CMS backend management system. 1.

2025-11-08