How to get the specified model or sub-categories under the parent category of the `categoryList` tag?

Calendar 👁️ 80

Ride AnQi CMScategoryListLabel: How to efficiently obtain the specified model and its subcategories under the parent category

In the daily operation of AnQiCMS, content classification is the core of the website structure, it not only relates to the smoothness of the user's browsing experience, but is also a key element that cannot be ignored in search engine optimization (SEO). In order to help everyone better manage and display website content, today we will delve into a very practical and powerful template tag in AnQiCMS.categoryListEspecially how to use it to obtain the list of child categories under a specified content model or a specific parent category.

AnQi CMS, with its efficient architecture based on the Go language and flexible content model design, provides an excellent content management solution for small and medium-sized enterprises and content operation teams. Understand and make good use of it.categoryListTags, they can help you build more dynamic and scalable website navigation and content structure.

categoryListOverview of tags: the foundation of content structure

categoryListThe main role of the tag is to help us extract classification data from the database and display it in a list format on the front-end page.It follows the syntax style of Django template engine, and can achieve various complex classification data calls through concise parameter configuration.

The most basiccategoryListThe usage is usually like this:

{% categoryList categories with moduleId="1" parentId="0" %}
    {# 在这里循环输出分类信息 #}
{% endcategoryList %}

Here, categoriesIt is a variable name defined by us, used to store the classified list of data obtained. The following content will be explained in detail on how to pass throughmoduleIdandparentIdThese two core parameters accurately obtain the sub-category data we want.

Accurately locate classification data: core parameter analysis

To obtain the specified content model or the subcategories under a specific parent category,categoryListThe tag provides two crucial parameters:moduleIdandparentId.

1. Specify the content model:moduleIdpower

The AnQi CMS has a major highlight, which is its 'flexible content model' feature, meaning you can create different content types according to business needs, such as 'articles', 'products', 'events', and so on.Each content model will have its own independent classification system.moduleIdThe parameters come into play.

For example, if you want to display all top-level categories under the "Article" model on the page, you need to know the corresponding "Article" model firstmoduleId(It is usually visible in the background content model management, and the system default article model ID is usually 1, the product model ID is 2). Then, you can use it like thismoduleId:

{% categoryList articleCategories with moduleId="1" parentId="0" %}
    {% for item in articleCategories %}
        <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
    {% endfor %}
{% endcategoryList %}

In this code block,moduleId="1"Make the system clearly understand that we only focus on the category data with ID 1 (i.e., the article model). If you do not specifymoduleIdThe system may attempt to retrieve all categories under all models, which may cause data confusion or not meet expectations in certain specific scenarios.

II. Retrieve the subcategories under the parent category:parentIdthe flexible application

the classification structure of the website is often multi-level, for example, under "News Center", there may be sub-categories such as "Company News" and "Industry Dynamics".parentIdThe parameter is used to control which level of child category you want to get.

  1. To get the top-level category:When you want to get all top-level categories without a parent category, you willparentIdis set to"0"Just do it.

    {% categoryList topCategories with moduleId="1" parentId="0" %}
        {# 循环输出所有文章模型的顶级分类 #}
    {% endcategoryList %}
    
  2. Get the direct subcategories of a specific parent category:This is one of the most common application scenarios. Assume that you have already obtained a parent category in a loop (for exampleitem), now you can list its direct subcategories below the parent category by usingparentId=item.Id. Here,item.Idis the ID of the current parent category in the outer loop.

    {% categoryList mainCategories with moduleId="1" parentId="0" %}
        {% for parentItem in mainCategories %}
            <h3><a href="{{ parentItem.Link }}">{{ parentItem.Title }}</a></h3>
            <ul>
                {% categoryList subCategories with parentId=parentItem.Id %}
                    {% for childItem in subCategories %}
                        <li><a href="{{ childItem.Link }}">{{ childItem.Title }}</a></li>
                    {% endfor %}
                {% endcategoryList %}
            </ul>
        {% endfor %}
    {% endcategoryList %}
    

    This code first retrieves all the top-level categories of article models, and then for each top-level category, it uses its ID asparentIdGet all subcategories at the next level. By nesting in this way, you can easily build a multi-level navigation menu.

  3. Get the direct subcategories of the current category page: If you are currently on a category list page (such as the "Company News" category page) and want to display the direct subcategories under the category, you can simply omit itparentIdParameters. The system will automatically judge the category ID of the current page and retrieve its direct subcategories.

    {# 假设当前页面是某个分类的列表页 #}
    {% categoryList currentSubCategories %}
        {% for item in currentSubCategories %}
            <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
        {% endfor %}
    {% endcategoryList %}
    
  4. Get the sibling categories (same-level categories) of the current category:In some cases, you may want to display other categories at the same level on a category page. In this case, you canparentIdis set to"parent"Please note that this usage is only valid on the current page when it is a category detail or list page.

    {# 假设当前页面是某个分类的列表页 #}
    {% categoryList siblingCategories with parentId="parent" %}
        {% for item in siblingCategories %}
            <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
        {% endfor %}
    {% endcategoryList %}
    

Practical tips: Building flexible navigation and content structure.

UnderstoodmoduleIdandparentIdThe usage, you can start building more flexible and diverse website layouts.

  • Multi-level navigation menu:Combineforloop anditem.HasChildren(Determine if the current category has subcategories) field, you can dynamically render a clear dropdown menu or sidebar navigation, greatly enhancing the user experience.

    `twig

Related articles

How to implement navigation between previous and next document sections using the `prevArchive` and `nextArchive` tags?

## Easily Navigate: How to Use `prevArchive` and `nextArchive` Tags for Smart Article Navigation in AnQiCMS As an experienced website operator, we know that website content is not just about publishing. How to allow users to smoothly browse through massive amounts of information and find the next article they are interested in is crucial for improving user experience, extending stay time, and even optimizing SEO. In the powerful and flexible content management system AnQiCMS (AnQiCMS)

2025-11-07

How to use the `archiveDetail` tag to get detailed information of a specific document?

As an experienced website operation expert, I know that content is the soul of the website, and how to efficiently and accurately present these contents is an indispensable part of our daily work.In such a powerful content management system as AnQiCMS, making good use of the template tags provided can greatly enhance our content operation efficiency.

2025-11-07

How to use the `archiveList` tag to get the document list in AnQiCMS template?

As an experienced website operations expert, and deeply familiar with AnQi CMS, I fully understand that how to efficiently obtain and display content is the core of building a website.Today, let's delve deeply into an extremely important and powerful tag in the Anqi CMS template, `archiveList`, which can help us flexibly obtain various document lists and provide endless possibilities for the presentation of website content.

2025-11-07

What are the functions of the 38 commonly used tags in AnQiCMS templates?

As an experienced website operations expert, I have a deep understanding of the powerful functions of AnQiCMS (AnQiCMS) and its flexible template system.It is not just a content management system, but also a tool that empowers enterprises and content operation teams, especially in terms of content display and management. Its core lies in a set of elegantly designed and feature-rich template tags system.Today, let's delve deeply into the 38 commonly used tags in the Anqi CMS template, see what roles they play, and how they help us easily build and manage all kinds of websites.

2025-11-07

How to call the document's Tag list with `tagList` tag?

## The Powerful Assistant for Content Operation: The Clever Use of `tagList` Tag in AnQiCMS In the vast sea of the Internet, how to efficiently organize information, make it easy to find, and ultimately reach the target users accurately is a problem that every content operator is thinking about. AnQiCMS (AnQiCMS), as a content management system designed for enterprises and self-media, understands this pain point and provides powerful tools such as `tagList` in its powerful template tag system, helping us easily master content tags and add the charm of classification and aggregation to the website.

2025-11-07

How to implement pagination display for the document list with `pagination` tag?

As an experienced website operations expert, I know that an efficient content management system (CMS) is the cornerstone of website operations.AnQiCMS boasts a simple and efficient architecture with rich features, performing excellently in content publishing and management.Today, let's delve deeply into a seemingly simple but extremely important feature in AnQiCMS—the `pagination` tag, which elegantly implements pagination display of document lists to optimize user experience and enhance website performance.

2025-11-07

What specific features does the 'Content Management' module of AnQiCMS backend contain?

As an experienced website operations expert, I know that content is the soul of the website, and an efficient, flexible content management system (CMS) is the core engine that drives the lifecycle of website content.AnQiCMS (AnQi CMS) performs excellently in this regard, and its "Content Management" module is undoubtedly one of the most powerful and favored features by operators.

2025-11-07

How to add a new document and set recommended attributes, keywords, and SEO titles in AnQiCMS?

## AnQiCMS Content Operation Manual: Easily add documents to achieve double benefits of SEO and user experience As an experienced website operation expert, I am well aware of the importance of an efficient and intelligent content management system for enterprises and self-media.AnQiCMS (AnQiCMS) is gaining popularity among operators with its high-performance architecture developed in Go language and rich content management features.It not only makes content publishing simple, but also deeply integrates SEO optimization into daily operations, helping your website stand out in the fierce market competition.

2025-11-07