How to display the child category list of a specified category in Anqi CMS

Calendar 👁️ 65

The organization of website content is crucial for user experience and information retrieval efficiency.A clear classification structure not only helps visitors quickly find the information they need, but also effectively enhances the professionalism of the website and user retention rate.In AnQiCMS (AnQiCMS), the flexible template tag system makes it very convenient to display the subcategory list of a specified category.

Core Tool:categoryListThe clever use of tags

To implement displaying the child category list of a specified category in AnQi CMS, we mainly usecategoryListTemplate tag. This tag is specifically used to retrieve the classification data of a website, and through its powerful parameter configuration, accurately locate the sub-category we need.

The AnQiCMS template syntax is similar to the Django template engine, variables are enclosed in double braces{{变量}}Define, logic control tags such asif/forthen use single braces and percent signs{% 标签 %}Define.

Understand the key parameters:moduleIdwithparentId

While usingcategoryListWhen labeling, there are two core parameters that you need to understand:

  1. moduleId(Model ID):AnQi CMS supports flexible content models such as article models, product models, etc. Each category belongs to a specific content model.moduleIdThe parameter is used to specify which content model category you want to retrieve.

    • You can view or create models in the "Content Management" -> "Content Model" section of the AnQi CMS backend, and find the corresponding model ID. In most cases, the article model ID is1, the product model ID is2. If you are not sure, you can check the model details.
  2. parentId(Parent category ID):This is a key parameter for controlling the hierarchy of categories.

    • WhenparentIdis set to0then,categoryListIt will retrieve all top-level categories under the specified model.
    • WhenparentIdWhen set to a specific category ID (for exampleparentId="123"), it will retrieve all direct child categories represented by that ID.
    • In addition,parentIdCan be set asparentIt is particularly useful on a category detail page, as it retrieves the sibling categories of the current category (i.e., other categories that share the same parent category as the current category).

Now, we are focusing on the topic of 'How to display the subcategory list of a specified category in AnQi CMS' and demonstrate it through actual template code exercises.

Practice exercise: Display the list of child categories of the specified category

Assuming you have a category whose ID is known (such as through URL parameters, or hardcoded in a template, or obtained through other tags), you want to display all the direct subcategories under this category.

This is a template code example that achieves this goal:

{# 假设您要获取ID为X的分类(例如文章模型下ID为5的分类)的子分类列表 #}

{# 首先,如果需要动态获取指定分类的ID,可以使用 categoryDetail 标签 #}
{% categoryDetail parentCategory with id="5" %} {# 这里的 "5" 就是您要指定的分类ID #}

{% if parentCategory %}
    <div class="subcategory-section">
        <h3>{{ parentCategory.Title }} 的子分类:</h3>
        <ul>
        {# 使用 categoryList 标签,通过 parentId 参数指定父分类ID #}
        {% categoryList subCategories with parentId=parentCategory.Id %}
            {% for item in subCategories %}
                <li>
                    <a href="{{ item.Link }}" title="{{ item.Title }}">
                        {{ item.Title }}
                        {# 如果需要显示子分类下有多少篇文章,可以使用 item.ArchiveCount #}
                        {% if item.ArchiveCount > 0 %}
                            ({{ item.ArchiveCount }})
                        {% endif %}
                    </a>
                </li>
            {% empty %}
                <li>目前没有子分类。</li>
            {% endfor %}
        {% endcategoryList %}
        </ul>
    </div>
{% else %}
    <p>抱歉,未找到您指定的父分类。</p>
{% endif %}

In this code block:

  • We first use{% categoryDetail parentCategory with id="5" %}Tag to get the specified ID (here is the5The category details. The advantage of this is that even if you only know one ID, you can also get the category title (parentCategory.Title) used for display and obtain the category ID (parentCategory.Id) for the next step.
  • Then,{% categoryList subCategories with parentId=parentCategory.Id %}Is the core. It tells Anqi CMS to look up the following.parentCategory.IdAll child categories of the parent category as.
  • {% for item in subCategories %}Loop through each of the child categories obtained.
  • {{ item.Link }}It will output the access link of the subcategory.
  • {{ item.Title }}It will output the name of the subcategory.
  • {{ item.ArchiveCount }}It can display how many documents are under this subcategory (if needed for your business).
  • {% empty %}Block is a very practical feature, whensubCategoriesWhen the list is empty, it will execute its content to avoid a blank page.

Advanced: Display of nested subcategories in multiple levels.

If your website category structure is relatively complex and you need to display multi-level subcategories, you can use nestingcategoryListand tags to combineitem.HasChildrenattributes to achieve this.item.HasChildrenIs a boolean value used to determine whether the current category still has subcategories.

{# 假设我们想显示文章模型(moduleId="1")下的所有顶级分类及其两级子分类 #}
{% categoryList level1Categories with moduleId="1" parentId="0" %}
    <ul class="main-categories">
        {% for level1 in level1Categories %}
            <li>
                <a href="{{ level1.Link }}">{{ level1.Title }}</a>
                {% if level1.HasChildren %} {# 判断一级分类是否有下级分类 #}
                    <ul class="sub-categories level-2">
                        {% categoryList level2Categories with parentId=level1.Id %} {# 获取当前一级分类的子分类 #}
                        {% for level2 in level2Categories %}
                            <li>
                                <a href="{{ level2.Link }}">{{ level2.Title }}</a>
                                {% if level2.HasChildren %} {# 判断二级分类是否有下级分类 #}
                                    <ul class="sub-categories level-3">
                                        {% categoryList level3Categories with parentId=level2.Id %} {# 获取二级分类的子分类 #}
                                        {% for level3 in level3Categories %}
                                            <li><a href="{{ level3.Link }}">{{ level3.Title }}</a></li>
                                        {% endfor %}
                                    </ul>
                                {% endif %}
                            </li>
                        {% endfor %}
                    </ul>
                {% endif %}
            </li>
        {% endfor %}
    </ul>
{% endcategoryList %}

This code demonstrates how to make recursive calls (even though it is explicit nesting).categoryListBuild multi-level category navigation.item.HasChildrenEnsured that the corresponding list will only be rendered when there are child categories.<ul>Thus maintaining the cleanliness of the page structure.

Practical suggestions

  • Template organization:In order to improve readability and maintainability, you can consider encapsulating the complex classification list logic into a separate template fragment (for examplepartial/sub_category_list.html),then use{% include %}Introduce tags where needed. This can prevent the main template file from becoming too bulky.
  • Performance consideration: In theory, you can nest any number of layers, but in practical applications, too deep category levels and too many nested loops may affect page loading performance.Suggest designing reasonably based on the actual website structure and performance requirements.
  • Style beautification:This code only provides the HTML structure. You need to combine it with CSS to style these lists, such as adding indentation, icons, hover effects, etc., to enhance the visual presentation.
  • Dynamically specify the category ID:In a real-world scenario, the category ID you specify may not be a fixed value, but one that is dynamically retrieved from the URL or automatically obtained from the current page context (such as on a category detail page). AnQi CMS will automatically identify the category information based on the current page URL, and you can omit it directly on the category detail pageidparameter,categoryDetailThe information of the current category will be automatically retrieved.

With these flexible template tags and parameters, Anqicms can help you easily build clear and powerful category navigation, effectively improving the content organization capabilities and user experience of the website.


Frequently Asked Questions (FAQ)

Q1: How can I display the list of all subcategories of the current article category on the article detail page?

A1: You can usearchiveDetailThe tag retrieves the category ID of the current article and then uses this ID asparentIdThe parameter is passed to `category'

Related articles

How does Anqi CMS implement the scheduled release of website content, so that it automatically displays at a specified time?

In content operations, efficiency and strategic thinking are the key to success.AnQiCMS (AnQiCMS) is well-versed in this field and offers a practical feature - scheduled content publishing, allowing website managers to accurately schedule the content release time, thus achieving automated operation and effectively enhancing the spread of content and website activity. ### Scheduling Posting: The Automation Tool for Content Operations Imagine that your website needs to publish industry news at a fixed time every morning, or plan a series of promotional articles in advance for specific holidays or marketing events. If relying on manual release by humans

2025-11-08

How to display the website logo and filing information in the AnQi CMS front-end template?

In website operation, Logo and filing information are not only the core of brand recognition, but also an important link for establishing user trust and complying with regulations.AnQi CMS took these key elements into consideration from the very beginning, providing intuitive settings and flexible template calling methods, allowing website operators to easily display this information on the front end.### Understanding AnqiCMS template mechanism Before delving into how to display the logo and filing information, it is very helpful to briefly understand the AnqiCMS frontend template mechanism.

2025-11-08

How can AnQi CMS ensure website content security, prevent malicious scraping, and protect the display of picture copyrights?

In the era of the explosion of digital content, the security of website content, the prevention of malicious collection, and the protection of image copyrights have become core challenges that website operators cannot ignore.A stable and reliable content management system should be able to provide a set of effective solutions.AnQiCMS (AnQiCMS) as an enterprise-level content management system provides multi-dimensional functional support in this aspect, helping users effectively protect their digital assets.Firstly, in terms of the overall security of the website content, Anqi CMS provides a solid foundation of protection.It is developed based on Go language

2025-11-08

How to customize fields and display them in Anqi CMS for different content models (such as articles, products)?

AnQi CMS with its flexible content model design makes the management and display of website content extremely powerful.In addition to common fields such as article title, publication time, and content body, we often need to add unique information for specific content types (such as products, news, cases, etc.).For example, a product may require “model number”, “color options”, and “size”, while news may require “source of the article” or “author introduction”.The customized field feature of AnQi CMS is designed to meet this personalized need

2025-11-08

How to set and display a custom template on a single page of AnQi CMS?

AnQi CMS, with its powerful flexibility and ease of use, has become a helpful assistant for many content operators.Especially in terms of content display, it provides us with rich customization space.For those web pages that require unique layouts or specific functions, such as "About Us", "Contact Us", or a special event landing page, we often hope that they can have a completely different visual style from ordinary articles or list pages.Aqii CMS provides a very convenient way for us to set exclusive custom templates for these single-page websites

2025-11-08

How to retrieve and display the document list under a specified Tag in AnQi CMS?

AnQiCMS (AnQiCMS) offers great convenience to website operators with its flexible content management and powerful template engine.In daily content operation, we often need to aggregate and display lists of related documents based on specific tags (Tag), such as 'Hot Topics', 'Technical Tips', or 'Product Reviews' and so on.This not only helps users quickly find the content they are interested in, but also effectively improves the internal link structure of the website, which is very beneficial for SEO.This article will delve into how to retrieve and display a list of documents under a specified tag in AnQiCMS

2025-11-08

How to display and manage the website message form of AnQi CMS and customize fields?

The website message form is an important link between users and the website, it not only helps us collect valuable opinions and suggestions, but also lays the foundation for various interactive functions such as user consultation and service reservation.AnQi CMS is well-versed in this, providing website operators with a straightforward, efficient, and highly customizable feedback form solution, making it exceptionally simple to manage and customize form fields.### Easily build the frontend presentation of the message form In Anqi CMS, to display the message form on the website page, you do not need to write complex backend logic, just insert the specific tags in the template file

2025-11-08

How to add and display friendship links on the Anqi CMS website?

In website operation, friendship links play an indispensable role.They not only help to improve the search engine ranking (SEO) of a website, but also bring potential traffic and improve the user experience, allowing visitors to get more information through related websites.AnQiCMS (AnQiCMS) understands this and therefore provides an intuitive and convenient friend link management feature, allowing you to easily add, manage, and display these important external connections.This article will introduce in detail how to add friend links in the Anqi CMS background, as well as how to flexibly display them on the website's front-end template

2025-11-08