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.