How to get all the subcategory lists under a specific category?

Calendar 👁️ 66

As a senior security CMS website operator, I know that flexible management of the website classification system is crucial for content organization and user experience.Especially when your website content becomes richer and the classification structure becomes complex, the ability to accurately and efficiently obtain the list of all subcategories under a specific category is an indispensable ability for template development and content display.The AnQi CMS provides a powerful and easy-to-use template tag system, making this operation very simple.

Flexible way to get all child categories under a specific category of Anqi CMS

In Anqi CMS, obtaining the list of child categories under a specific category mainly depends on the core ofcategoryListTemplate tag. This tag is designed to help you retrieve category data under articles, products, or other custom content models and provide parameters to accurately locate the data range you need.

Core tags:categoryListFunction Analysis

categoryListThe tag is the starting point for operating category data. It can not only list the top-level categories but can also retrieve the direct subcategories layer by layer according to the specified parent category ID, and even realize the display of multi-level nested category categories.Its basic usage is{% categoryList 变量名称 with 参数 %}of which变量名称Customized by you, used to refer to the returned category list data within the tag block.

ByparentIdParameter specifies the parent category

To get the list of subcategories under a specific category, the most critical parameter isparentId. You can use it in the following ways:

  • Get the top-level categoryIf you want to list all top-level categories without a parent, you canparentIdis set to0. Since category data is associated with the content model, you also need to go throughmoduleIdThe parameter specifies which content model's top-level category is specified (for example, the article model ID is 1, the product model ID is 2).

    {% categoryList categories with moduleId="1" parentId="0" %}
        {# 在这里遍历顶级分类 #}
    {% endcategoryList %}
    
  • Get the direct subcategories of the specified category.If you know the ID of a category and want to get its direct subcategories, you can directly assign the ID toparentIdFor example, you can do this operation to get all direct subcategories under the category with ID 10:

    {% categoryList subCategories with parentId="10" %}
        {# 在这里遍历ID为10的分类的直接子分类 #}
    {% endcategoryList %}
    
  • Get the subcategories of the current category on the category detail page.When you are on the detail page of a category, Anqin CMS can automatically recognize the context of the current category. Therefore, if you omitparentIdparameter,categoryListThe label will default to getting the direct subcategories of the current category.

    {# 假设当前页面是某个分类的详情页 #}
    {% categoryList currentCategorySubCategories %}
        {# 在这里遍历当前分类的直接子分类 #}
    {% endcategoryList %}
    

Implement a multi-level nested subcategory list

In actual operation, the category structure is often not just two layers. Anqi CMS'scategoryListThe tag combined with the template engine's looping nesting capability can be very flexible in building a multi-level subcategory list of any depth. By determining whether each category item has a subcategoryHasChildrenField), we can achieve dynamic recursive display.

Here is a typical example of implementing a three-level classification nesting code, which shows how to start from the top-level classification and delve into its sub-classifications layer by layer:

{% categoryList topCategories with moduleId="1" parentId="0" %}
{# 一级分类列表 #}
<ul>
    {% for level1Category in topCategories %}
    <li>
        <a href="{{ level1Category.Link }}">{{ level1Category.Title }}</a>
        {% if level1Category.HasChildren %} {# 判断当前一级分类是否有子分类 #}
            {% categoryList level2Categories with parentId=level1Category.Id %}
            {# 二级分类列表 #}
            <ul>
                {% for level2Category in level2Categories %}
                <li>
                    <a href="{{ level2Category.Link }}">{{ level2Category.Title }}</a>
                    {% if level2Category.HasChildren %} {# 判断当前二级分类是否有子分类 #}
                        {% categoryList level3Categories with parentId=level2Category.Id %}
                        {# 三级分类列表 #}
                        <ul>
                            {% for level3Category in level3Categories %}
                            <li>
                                <a href="{{ level3Category.Link }}">{{ level3Category.Title }}</a>
                                {# 如果需要更深层级,可以继续嵌套 #}
                            </li>
                            {% endfor %}
                        </ul>
                        {% endcategoryList %}
                    {% endif %}
                </li>
                {% endfor %}
            </ul>
            {% endcategoryList %}
        {% endif %}
    </li>
    {% endfor %}
</ul>
{% endcategoryList %}

categoryListParsing of the fields returned by the tag

IncategoryListIn the loop of tags, eachitemis in the examplelevel1Category/level2Category/level3Categoryetc.") all contain a series of useful classification information, you can access it directly throughitem.字段名in the way:

  • Id: The unique identifier of the classification.
  • Title: The title name displayed on the front end.
  • Link: The URL link corresponding to the category.
  • Description: The introduction or description of the category.
  • ParentId: The parent category ID of the current category.
  • HasChildrenA boolean value indicating whether the current category contains subcategories. This is the key judgment basis for implementing multi-level nesting.
  • Logo/ThumbThe cover image and thumbnail of the category for visual display.
  • ArchiveCount: The number of documents under the current category.

By using these fields, you can build a rich and diverse category navigation, category block, or content list, greatly enhancing the content organization capabilities and user browsing experience of the website.

Frequently Asked Questions

Q1: How to determine if a category has subcategories in the template?

You can use it tocategoryListTag returns the category items inHasChildrenfield to determine. This is a boolean value, if it istrueThis indicates that there are subcategories under the category; if not,falseThis indicates that there are no subcategories. This is very useful when building multi-level navigation, as it can control whether to display submenus or expand arrows.

Q2: How do I get the subcategories under different content models?

categoryListTag supportmoduleIdParameter, you can use it to specify the content model ID of the category you want to query. For example,moduleId="1"It is usually used for article models,moduleId="2"Used for product models. When retrieving subcategories, make sure yourmoduleIdparameters match the content model you want to operate.

Q3: How can I list the first-level child categories under a specified parent category without displaying deeper nesting?

If you only want to display the direct child categories of a certain parent category without recursively displaying deeper-level subcategories, then incategoryListwithin the tag,forthe loop, you can directly useparentIdParameters to obtain its child level and omitif item.HasChildrenJudgment and internal nestingcategoryListLoop. For example,{% categoryList subCategories with parentId=某个已知ID %}Will only return the direct child categories under the ID and will not automatically obtain the grandchild categories.

Related articles

How to implement batch replacement of keywords or links in website article content?

As a long-term深耕 website operator who has a deep understanding of AnQiCMS (AnQiCMS), I know that content is the lifeline of the website, and efficient content management is the key to success.In daily operations, we often encounter the need to update keywords or links in a large number of articles on the website, which may be due to brand strategy adjustments, SEO optimization needs, changes in links from external cooperative partners, or even legal and regulatory requirements.Facing thousands of articles, manually modifying one by one is undoubtedly a nightmare that consumes time and effort.It's lucky that

2025-11-06

How to create reusable code snippets (macro functions) in templates?

As an experienced AnQiCMS website operator, I know that efficiency and consistency are very important in daily content management.Especially in template design and content publishing, if code can be effectively reused, it can not only greatly improve work efficiency, but also ensure the consistency of website visual and functional integrity.Today, I will discuss with everyone how to create and utilize reusable code snippets, also known as macro functions, which are one of the key tools for our efficient operation.### Template code reuse

2025-11-06

How to unify the overall layout and structure of the website through the template inheritance mechanism?

As an experienced CMS website operation person, I know the importance of an efficient and unified template system for website operation.AnQi CMS, with its high-performance architecture based on the Go language and flexible template system, provides us with powerful tools to ensure the consistency of content display, the unity of brand image, and greatly improve the efficiency of content management and publication.The importance of unified website layout and structure Maintaining consistency in the overall layout and structure of a website is crucial for attracting and retaining users in website operations.a mess

2025-11-06

How to directly call the built-in methods of Go struct in the template?

As a professional who has been deeply engaged in the content operation of Anqi CMS for a long time, I know that the template plays a core role in the presentation of website content.A highly efficient and flexible template system that can greatly enhance the efficiency and richness of content publishing.AnQi CMS, with its powerful backend based on Go language, provides template developers with the ability to directly interact with the underlying data structure, one very practical feature being the ability to directly call built-in methods defined in Go structs in the template.

2025-11-06

How to get the detailed content and images of a single page (such as "About Us")?

Managing and displaying a single page like "About Us" and its content and images in Anqi CMS is an efficient and flexible process.As an experienced website operator, I will explain in detail how to create from the backend to the frontend display, and fully master this feature.--- **To get the detailed content and images of a single page (such as "About Us") in AnQiCMS** AnQiCMS (AnQiCMS) provides a dedicated management module called "Single Page" for independent pages such as "About Us" and "Contact Us"

2025-11-06

How to deploy AnQiCMS multi-site in Docker environment and configure reverse proxy?

As an experienced CMS website operation person, I fully understand the importance of an efficient and flexible content management system for enterprises and self-media in the increasingly complex network environment.AnQi CMS, with its high-performance architecture based on the Go language, rich feature set, and SEO-friendliness, has become the ideal choice for many users.Especially in multi-site management, AnQiCMS provides a simple and efficient solution.

2025-11-06

How to ensure the security of AnQiCMS deployed websites and prevent common security issues?

As a website operator who has dealt with AnQiCMS for many years, I know the importance of website security for content operation.A secure website not only protects user data and maintains the reputation of the enterprise, but is also the foundation for the stable release and dissemination of content.AnQiCMS was designed with security in mind from the outset, and provides a variety of built-in features and practical suggestions to help us build and maintain a secure and stable content platform.AnQi CMS is developed using Go language, which lays a solid foundation for the system's running efficiency and stability

2025-11-06

How to correctly render Markdown formatted content in AnQiCMS templates?

In AnQiCMS templates, correctly rendering Markdown content is a key factor for website operators to enhance content presentation and editing efficiency.Markdown is a lightweight markup language that is favored by content creators for its simplicity, readability, and ease of writing.The AnQiCMS system not only supports the creation of Markdown content but also provides a flexible template rendering mechanism to ensure that this content is presented in the expected way on the front-end page, and can even integrate third-party libraries to display mathematical formulas and flow charts.###

2025-11-06