In AnQiCMS, you can easily display the subcategory list under the category page with flexible template tags.It is crucial for building a clear website structure, enhancing user navigation experience, and optimizing search engine crawling efficiency.AnQiCMS has a powerful template system and its Django-style tag syntax, making the display of this dynamic content intuitive and efficient.

To implement the display of the subcategory list under the category page, the following steps are mainly involved:

1. Understand the template structure and tag usage of AnQiCMS

AnQiCMS template files are usually named with.htmlsuffix and stored in/templatedirectory. It uses a syntax similar to Django template engine, with variables enclosed in double curly braces{{变量}}Comma, conditional judgment and loop control tags use single curly braces and percent signs{% 标签 %}On the category page, the core lies in using the provided by AnQiCMScategoryList.

Generally, category pages use model-specific list templates, such as the category list page for the article model might bearticle/list.htmlor a custom template file for a specific category that you have created in the backend.

Second, get the current category information and model ID

On the category list page, you first need to obtain some basic information about the current category, especially its ID and the ID of the model it belongs to. This can be done bycategoryDetailtags. AlthoughcategoryListWhen calling on the current category page, it can usually intelligently obtain its subcategories, but explicitly obtain the ID and model ID of the current category and pass them tocategoryListLabels can make the code more robust and readable.

For example, you can get the details of the current category at the appropriate location in your category template file:

{# 假设您正在一个分类的列表页,并且希望显示当前分类的子分类。 #}

{# 1. 首先,获取当前分类的详细信息,这能帮助我们确定模型ID以及作为父ID。 #}
{% categoryDetail currentCategoryDetail %}
    {# 此时,currentCategoryDetail 对象包含了当前分类的所有信息,例如: #}
    {# {{ currentCategoryDetail.Title }} - 当前分类的标题 #}
    {# {{ currentCategoryDetail.Id }} - 当前分类的ID #}
    {# {{ currentCategoryDetail.ModuleId }} - 当前分类所属模型的ID #}
    {# 您可以根据需要显示这些信息,例如作为页面标题或面包屑导航的一部分。 #}
    <div class="current-category-info">
        <h1>{{ currentCategoryDetail.Title }}</h1>
        <p>{{ currentCategoryDetail.Description }}</p>
    </div>

    {# 接下来,我们就可以使用这些信息来获取其下属子分类。 #}
{% endcategoryDetail %}

3. UsecategoryListLabels to display subcategory lists

Get the current categoryIdandModuleIdAfter that, you can callcategoryListThe tag to get the direct subordinate child categoriescategoryListThe tag supports multiple parameters, includingparentIdandmoduleIdIt is the key.

  • parentIdSet to the parent category ID you want to retrieve the subcategory. That is, the one above.currentCategoryDetail.Id.
  • moduleIdSpecify the ID of the content model you belong to. This can also be obtained fromcurrentCategoryDetail.ModuleIdGet, ensure that you are getting the correct model's subcategory.

This is a complete example showing how to obtain and display the list of sub-categories under a category page, while also considering the possibility that sub-categories may contain deeper-level sub-categories:

`twig {# Assume you are on a category list page and want to display the subcategories of the current category. #}

{# 1.First, obtain the detailed information of the current category, which helps us determine the model ID and the parent ID.

<div class="current-category-info">
    <h1>{{ currentCategoryDetail.Title }}</h1>
    <p>分类简介:{{ currentCategoryDetail.Description }}</p>
    {# 更多当前分类的信息可以在这里显示,如Banner图、缩略图等 #}
    {%