As an experienced CMS website operation personnel, I know that flexible management of the website classification system is crucial for content organization and user experience.Especially when the content of your website becomes richer and the classification structure becomes complex, the ability to accurately and efficiently obtain a list of all subcategories under a specific category is an indispensable ability for template development and content display.The Auto CMS provides a powerful and easy-to-use template tag system, making this operation very simple.
Flexible way to get the list of all subcategories under a specific category of Anqi CMS
In Auto CMS, retrieving the list of subcategories under a specific category mainly relies on the corecategoryListTemplate tags. 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 parsing of the English
categoryListLabels are the starting point for operating on category data.It can not only list the top-level categories but also retrieve their direct subcategories layer by layer according to the specified parent category ID, and even realize the display of multi-level nested categories.{% categoryList 变量名称 with 参数 %}where,变量名称is the one you customized, used to reference the returned category list data within the tag block.
PassparentIdparameter 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 category:If you want to list all top-level categories without parent categories, you can
parentIdset0. Since category data is associated with the content model, you also need to go throughmoduleIdThe parameter specifies which content model's top-level category it is (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 that ID to
parentIdFor example, to get all direct subcategories under the category with ID 10, you can operate as follows:{% categoryList subCategories with parentId="10" %} {# 在这里遍历ID为10的分类的直接子分类 #} {% endcategoryList %}On the category details page, you can get the subcategories of the current categoryWhen you are on the detail page of a category, Safe CMS can automatically identify the context of the current category. Therefore, if you omit
parentIdparameters,categoryListTags will default to get the direct subcategories of the current category.{# 假设当前页面是某个分类的详情页 #} {% categoryList currentCategorySubCategories %} {# 在这里遍历当前分类的直接子分类 #} {% endcategoryList %}
Implement multi-level nested subcategory lists
In actual operation, the classification structure is often not just two layers. Anqi CMS'scategoryListThe tag combined with the template engine's nested loop capability can be very flexible to build multi-level subcategory lists of any depth. Through judging whether each category item has a subcategory (HasChildrenField), we can achieve dynamic recursive display.
The following is an example of typical code that implements three-level classification nesting, which shows how to start from the top-level category and delve into its subcategories 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
IncategoryListThe loop of tags, each oneitem(In the example is)level1Category/level2Category/level3Categoryetc.) all contain a series of useful classification information, you can access it directly throughitem.字段名:
Id: The unique identifier ID of the category.Title: The title name displayed in the front-end classification.Link: Classification corresponding URL link.Description: Brief introduction or description of the category.ParentId: Parent category ID of the current category.HasChildren: An English boolean value indicating whether the current category contains subcategories. This is a key judgment basis for implementing multi-level nesting.Logo/Thumb: The cover image and thumbnail of the category, used for visual display.ArchiveCount: 当前分类下的文档数量。
Through these fields, you can build rich and diverse classification navigation, classification blocks, or content lists, greatly enhancing the website's content organization capabilities and user browsing experience.
Frequently Asked Questions
Q1: How to determine if a category has subcategories in the template?
You can usecategoryListReturns categories in the tag.HasChildrenfield to determine. This is a boolean value, if it istrue, it indicates that there are subcategories under this category; if it isfalse, it indicates that there are no subcategories. This is very useful when building multi-level navigation, as it can control whether to display the sub-menu or the expand arrow.
Q2: How can 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"English for articles model,moduleId="2"For product model. When getting subcategories, make sure yourmoduleIdparameters match the content model you want to operate.
Q3: How can I list only the first-level child categories under a specified parent category, without displaying deeper nested ones?
If you only want to display the direct child categories of a certain parent category without displaying deeper-level subcategories recursively, then you can directly usecategoryListWithin the tags,forin the loop,parentIdParameters to get its sub-levels and omitif item.HasChildrenthe judgment and internal nestingcategoryListloop. For example,{% categoryList subCategories with parentId=某个已知ID %}will only return the direct sub-categories under the ID and will not automatically get the grandchild categories.