How to get the list of all direct subcategories of the current page category?

Calendar 👁️ 61

As an experienced website operations expert, I am well aware that how to efficiently organize and display the classification structure is crucial when managing a content-rich website.AnQiCMS (AnQiCMS) with its powerful template tag system provides us with great flexibility, making it easy to achieve various complex classification display requirements.Today, let's delve into a common and practical scenario: how to retrieve and display the list of all direct child categories under the current page category.

Understand the classification system of Anqi CMS

In Anqi CMS, the core of content organization lies in its flexibility,Content modelsuch as articles, products, etc.), and based on these models,Category. Categories exist in a tree structure, which means each category can have its own parent category and multiple child categories, forming a clear hierarchical relationship.This structure allows us to create dedicated classification methods for different types of content, greatly enhancing the manageability of content and the convenience of user browsing.Whether it is the main navigation of the website, the sidebar menu, or the breadcrumb navigation, categories play an indispensable role.

clear goal: obtain the direct subcategories of the current category

Our goal is very clear: when we visit a category page, we hope to dynamically display all the next-level subcategories under the category.For example, if the current category is "News Center", we may want to display subcategories such as "Company News" and "Industry Dynamics";If the current is 'Product Category', we want to list 'Electronic Products', 'Home Goods', and other direct subcategories.

To achieve this goal, we need to take two steps: first, determine the category ID of the current page; second, use this ID to query and list its direct subcategories.

The first step: obtain the identity identifier of the current category - Category ID

In the template design of Anqi CMS, to obtain the detailed information of the current page category, we can take advantage of the powerfulcategoryDetailLabel. This label is specifically used to retrieve detailed data for a specified category, and if we do not provide it withidortokenparameters, it will very intelligently automatically identify and retrieve the category information of the current page.

To facilitate subsequent queries for its subcategories, what we need most is the current category'sId. We can assign the ID of the current category to a template variable like this:

{% categoryDetail currentCategoryId with name="Id" %}

here,currentCategoryIdIs a variable that contains the current category ID. In this way, no matter where we are on the category page, we can accurately obtain its unique identifier.

The second step: ridecategoryListLabel, list direct subcategories

With the current category ID, the next step is to use it to find its direct subcategories. At this point,categoryListthe label comes into play.categoryListTags allow us to retrieve category lists based on specific conditions (such as the content model, parent category ID, etc.).

Its core parameters include:

  • moduleId: Specify the content model belonging to the category. For example, the article model usually corresponds to the ID is1, the product model may correspond2. This parameter is very important because it specifies the content model from which we want to retrieve the classification.If you are unsure of the specific model ID, you can check it in the "Content Management" -> "Content Model" in the backend.
  • parentId: This is the key to achieving our goal. We just gotcurrentCategoryIdAssign toparentId, can accurately tell the system: "Please list the direct subcategories under the category with IDcurrentCategoryId!"}
  • limit: Controls the number of subcategories returned, you can limit the number of displayed items as needed, for examplelimit="10".
  • siteIdIf your AnQiCMS has deployed multiple sites and you want to get the category of a specific site, you can specify this parameter.In most cases, if you only manage one site, it is not necessary to fill in.

Now, let's combine these two steps to build a complete code snippet:

{% categoryDetail currentCategoryId with name="Id" %}
{% categoryList childCategories with moduleId="1" parentId=currentCategoryId %}
    {% for item in childCategories %}
        <a href="{{ item.Link }}" title="{{ item.Title }}">
            {{ item.Title }}
        </a>
        {# 进一步判断是否有孙子分类,并显示 #}
        {% if item.HasChildren %}
            <ul>
                {% categoryList grandChildCategories with parentId=item.Id %}
                    {% for grandChild in grandChildCategories %}
                        <li><a href="{{ grandChild.Link }}">{{ grandChild.Title }}</a></li>
                    {% endfor %}
                {% endcategoryList %}
            </ul>
        {% endif %}
    {% empty %}
        <p>当前分类暂无直接子分类。</p>
    {% endfor %}
{% endcategoryList %}

In this code block:

  1. We first usecategoryDetailRetrieve the category ID of the current page and store it incurrentCategoryIdthe variable.
  2. Next, we callcategoryList, specifymoduleIdWith1(assuming we are dealing with article categories), and assignparentIdis set tocurrentCategoryIdThus,childCategoriesthe variable will contain all direct subcategories of the current category.
  3. By{% for item in childCategories %}Loop, we iterate through these subcategories one by one and can utilizeitem.LinkGet the category link,item.TitleGet the category name, and easily build a subcategory navigation list.
  4. We have also added a{% if item.HasChildren %}The judgment, this is very practical.item.HasChildrenIt will return a boolean value indicating whether the current subcategory has any further subcategories (i.e., grandchild categories).This allows you to decide whether to further display a deeper level of classification structure.
  5. Finally,{% empty %}The tag provides a friendly user experience, and it will display a prompt when no subcategories are found, avoiding a blank page.

Optimization and Expansion Suggestions

  • Style beautification: The above code only demonstrates the logical structure, you can beautify the display style of these sub-categories according to your design needs through CSS.
  • Hierarchical DisplayIf you need to display multi-level subcategories, you can in{% if item.HasChildren %}nested inside againcategoryListthe tag and use it.parentIdis set toitem.IdThis pattern can be used to implement an infinite-level classification navigation. However, for the user experience, it is usually not recommended to display too deep levels.
  • Flexible control of the display quantity: Make good use of `limit

Related articles

What is the specific function and application scenario when the `parentId` parameter of `categoryList` is set to "0"?

AnQiCMS (AnQiCMS) is an efficient and flexible content management system, whose powerful template tag system is the key to content operators achieving personalized display.Among many practical tags, the `categoryList` tag is undoubtedly an important tool for building the framework of a website.Today, let's delve into the `categoryList` tag and its seemingly simple yet extraordinary parameter `parentId="0"`. What specific role and application scenarios does it have?

2025-11-06

How to implement nested display of multi-level categories in AnQiCMS templates, such as a three-level menu?

In modern web design, a clear and intuitive navigation system not only greatly enhances user experience but is also an indispensable part of search engine optimization (SEO).A multi-level classification menu, especially a three-level nested menu, can effectively organize a large amount of content, allowing users to quickly locate the information they need, and also helps search engines understand the hierarchical structure of the website.As an experienced website operations expert, I am well aware that implementing such a feature in AnQiCMS (AnQi CMS) is both simple and efficient.

2025-11-06

How to only display categories under specific content models (such as articles or products) in `categoryList`?

As an experienced website operations expert, I know that the flexibility of the Content Management System (CMS) is crucial for efficient operations.AnQiCMS (AnQiCMS) performs exceptionally well in this area with its powerful content model features.Today, let's delve into a common requirement: how to use the `categoryList` tag to accurately display only the categories under specific content models (such as articles or products).

2025-11-06

How to use the `categoryList` tag to get the list of all top-level categories?

HelloAs an experienced website operations expert, I am more than happy to give you a detailed explanation of the powerful `categoryList` tag in AnQi CMS, especially on how to accurately obtain the top-level category list on your website.Understand and apply this tag, it will be a key step in building a clear and efficient website navigation system. --- ## Practical Anqi CMS: Skillfully Using `categoryList` Tag to Easily Display Website Top-level Category Navigation In a content management system, categories are the core framework for organizing information and guiding users' browsing.

2025-11-06

Does the `categoryList` tag support limiting the number of categories returned? How to set the `limit` parameter?

As an experienced website operations expert, I know how important it is to have precise control over content display when building and optimizing a website.AnQiCMS as an efficient and flexible content management system, its powerful template tag system provides us with abundant operating space.Today, let's delve into the function of the `categoryList` tag in limiting the number of returned categories, as well as how to巧妙ly use the `limit` parameter.

2025-11-06

How do I correctly use the `all=true` parameter of `categoryList` to display all categories in the sidebar?

In Anqi CMS, efficiently managing and displaying website content is the key to operational success.The sidebar acts as an important navigation area for websites, often needing to display clear categories to guide users quickly to the information they need.When you want to display all categories in the sidebar and are puzzled by the `all=true` parameter of the `categoryList` tag, this is exactly the topic we will delve into today.In AnQi CMS, categories are the framework for organizing website content.

2025-11-06

What do `categoryList` loop's `item.Title` and `item.Link` represent, and how to output?

As an experienced website operations expert, I know that how to efficiently and accurately extract and display data in a content management system is the cornerstone of website success.AnQiCMS is an enterprise-level content management system based on the Go language, and its simple and efficient template engine is very appealing to me.Today, let's delve deeply into what `categoryList` loop's `item.Title` and `item.Link` represent in AnQiCMS template development, and how we can elegantly present them in the template.### Reveal

2025-11-06

How can you determine if a category has a subcategory? How can the `item.HasChildren` field be applied to conditional judgments?

As an experienced website operations expert, I deeply understand the importance of efficiently using template tags in the daily application and content strategy formulation of AnQiCMS (AnQiCMS).Today, let's delve deeply into a very practical topic in website structure construction: how to determine whether a category (Category) contains subcategories, and how to cleverly use the `item.HasChildren` field in templates to achieve intelligent content display.

2025-11-06