How to get all top-level categories under a specified model for the `categoryList` tag?

Calendar 👁️ 46

AnQi CMS, as an efficient and customizable enterprise-level content management system, provides great flexibility in content organization and display.Among them, skillfully using template tags is the key to leveraging its potential.categoryListThe tag, which accurately retrieves all top-level categories under a specified content model, is crucial for building clear website navigation, content modules, or product display pages.

Understand the content model and classification system of AnQi CMS

In AnQi CMS, one of the core concepts of content management is the "content model".It allows us to create different types of content structures based on business needs, such as "article modelEach content model can have its own independent classification system, ensuring that content is neatly categorized without interference.

Classification, as the name implies, is the hierarchical structure of organizing content.Top categories are the starting point of the classification system, usually representing the major sections of a website or major product categories.For example, under the 'Product Model', the top categories may be 'Mobile Phone', 'Computer', 'Household Appliances'; and under the 'Article Model', it may be 'Industry News', 'Technical Sharing', 'Company Dynamics'.Accurately obtain these top-level categories is a common requirement for building website navigation and homepage content blocks.

categoryListTags: The tool to get categories

AnQi CMS template engine provides a wealth of tags, includingcategoryListThe tag is specifically used to retrieve the category list. Its strength lies in being able to finely control the range of categories to be retrieved through a series of parameters.

To obtain all top-level categories under the specified content model, we need to focus oncategoryListThe two core parameters of the tag:moduleIdandparentId.

1.moduleId:Locate the target content model

moduleIdThe parameter tellscategoryListLabel, which content model do you want to get categories from.Each content model created in the AnQi CMS backend has a unique numeric ID.moduleIdIt could be1,“The product model”smoduleIdIt could be2The specific ID should be set according to your backend settings.

If you do not specifymoduleIdThe tag may try to obtain the category from the current page context or the default model, which is often not the result we want. By explicitly settingmoduleIdWe can accurately limit the search range to a specific content model.

2.parentId="0": Filter out the top-level categories

parentIdParameters are used to specify the ID of the parent category. In the Anqi CMS category system, the top-level categoryparentIdis fixed to0. This means, anyparentIdWith0The category, all of which do not have a parent category, are at the top of the category hierarchy.

Therefore, when we willparentIdis set to"0"then,categoryListTags will only return all first-level categories under the specified model, effectively filtering out all subcategories.

categoryListThe basic syntax structure of tags

categoryListThe general structure of tags is as follows:

{% categoryList 变量名称 with 参数 %}
    {# 循环输出分类列表的代码 #}
{% endcategoryList %}

Here:

  • 变量名称: You can customize a variable name (such ascategories), used to store the classified data obtained, and thenforloop through this variable.
  • with 参数: Here you canmoduleIdandparentIdset parameters such as.
  • {% endcategoryList %}: The end tag, cannot be omitted.

Actual operation: Get all top-level categories under the specified model.

Assuming we want to obtain all the top-level categories under the 'Product Model' and display them in some area on the website homepage. First, we need to know the 'Product Model'moduleId. In the AnQi CMS backend, go to 'Content Management' -> 'Content Model', you can usually see the list of models and their corresponding IDs. Assume the ID of the 'Product Model' is2.

So, the code can be written like this:

{# 假设产品模型的moduleId是2 #}
<nav class="product-categories-nav">
    <h3>产品分类</h3>
    <ul>
        {% categoryList productTopCategories with moduleId="2" parentId="0" %}
            {% for item in productTopCategories %}
                <li>
                    <a href="{{ item.Link }}" title="{{ item.Title }}">
                        {{ item.Title }}
                    </a>
                    {# 如果需要显示该顶级分类下的子分类,可以根据item.HasChildren判断后,在此处再次嵌套categoryList标签 #}
                    {% if item.HasChildren %}
                        <ul class="sub-categories">
                            {# 嵌套获取子分类,例如:{% categoryList subCats with parentId=item.Id %}{# ... #}{% endcategoryList %} #}
                        </ul>
                    {% endif %}
                </li>
            {% endfor %}
            {% empty %}
                <li>暂无顶级产品分类。</li>
        {% endcategoryList %}
    </ul>
</nav>

In this code block:

  1. {% categoryList productTopCategories with moduleId="2" parentId="0" %}: We specifymoduleId="2"To lock the product model, and throughparentId="0"Ensure that only the top-level categories are obtained. The obtained category list will be stored inproductTopCategoriesthe variable.
  2. {% for item in productTopCategories %}: We traverseproductTopCategoriesthe list,itemThe variable represents an independent category object in each loop.
  3. {{ item.Link }}and{{ item.Title }}: This is a category objectitemCommon fields used, respectively for outputting the link address and title of the category. Moreover,itemalso includesId(Category ID),Description(Category Description),HasChildren(Whether it includes subcategories) and other rich information, you can flexibly call according to your needs."),
  4. {% empty %}: This is a very practicalforLoop auxiliary tag. WhenproductTopCategoriesWhen the list is empty, it will output{% empty %}and{% endfor %}To avoid blank spaces or errors on the page
  5. {% if item.HasChildren %}: It is determined through judgmentitem.HasChildrenField, can tell whether the current top-level category contains subcategories. If you need to display subcategories under the top-level category, you can continue to use this conditional blockcategoryListLabel, andparentIdis set toitem.IdTo achieve nested display of multi-level categories.

Insights from content operation.

Mastering how to obtain the top-level categories under a specified model has profound significance for the content operation of the website:

  • Build a clear navigation structure: The main navigation of a website is usually composed of top-level categories, allowing users to easily find the content they need.
  • Optimize the layout of the homepage contentDisplay different content models' top categories on the homepage, which can guide users to quickly enter the sections they are interested in, improving the page stay time.
  • Improve SEO friendliness:A clear classification structure helps search engines better understand the hierarchy of website content, improve inclusion efficiency and keyword ranking.
  • Flexible modular displayCan be configured with different top category displays for different models, making the website content layout more diverse and professional.

Summary

Of Security CMScategoryListLabel combinationmoduleIdandparentId="0"Parameters provide a concise and powerful way to obtain all top-level categories under the specified content model.This is not only a technical implementation, but also a fundamental capability of content organization and user experience optimization.By using it flexibly, you will be able to build a website that is more logical and easier for users to explore.


Frequently Asked Questions (FAQ)

Q1: I have set upmoduleIdandparentId="0"But why are no categories displayed? A1:This could have several reasons. First, please check the specifiedmoduleIdwhether

Related articles

How to create custom fields for different content models (such as articles, products) in AnQiCMS?

Good, as an experienced website operation expert, I am very willing to deeply analyze the powerful functions and actual operations of the customized fields in AnQiCMS. --- ## In AnQiCMS, create custom fields for different content models: unlock the powerful engine of personalized content In the era where content is king, the diversity and personalization of website content are key to attracting and retaining users.A remarkable content management system (CMS) should never limit you to a fixed content structure.AnQiCMS understands this way

2025-11-06

How does AnQiCMS handle the management of pseudo-static and 301 redirection to solve the problem of URL structure optimization and traffic loss?

## AnQiCMS' Static URL and 301 Redirect: A Smart Solution for URL Optimization and Traffic Protection In the digital wave, a website's URL (Uniform Resource Locator) is far more than a simple address.It is not only the path that users access the page, but also the key clue for search engines to understand the content of the website and evaluate its value.A clear and semantically friendly URL can significantly enhance user experience, improve search engine crawling efficiency and ranking performance.

2025-11-06

How to use AnQiCMS advanced SEO tools (such as Sitemap, Robots.txt) to improve the visibility of a website in search engines?

## Optimize Your Search Engine Footprint: How AnQiCMS Harnesses Sitemap and Robots.txt to Improve Website Visibility In the ever-changing digital world, the search engine visibility of a website is the foundation of its success.No matter if you are running a small and medium-sized enterprise, a self-media platform, or managing multiple sites, ensuring that your content can be discovered and effectively indexed by search engines is crucial for traffic acquisition and brand exposure.

2025-11-06

How does AnQiCMS's document content collection and batch import function improve the efficiency of content construction?

## AnQiCMS: Content collection and batch import, how to drive the leap in content construction efficiency?In today's digital age, website content has become the core of communication between enterprises and users, brand building, and search engine optimization.However, starting from scratch, creating, managing, and updating a massive amount of content is a continuous and time-consuming task for any website operator.The cumbersome collection of materials, manual input, and repeated adjustments not only consume a lot of human and material resources, but may also miss valuable market opportunities.

2025-11-06

how the `categoryList` tag implements nested hierarchical display of multi-level categories?

Sure, as an experienced website operations expert, I am happy to deeply analyze how to implement nested display of multi-level categories in the `categoryList` tag of AnQi CMS and transform it into an article that is easy to understand and practical. --- ## AnQi CMS `categoryList` Tag: Easily Implement Nested Display of Multi-level Categories, Making the Website Structure Clear and Orderly A clear classification system is crucial for user experience (UX) and search engine optimization (SEO) in website operations.

2025-11-06

How to get the link, Logo image, and document count in the `categoryList` loop?

As an experienced website operations expert, I am well aware of the importance of an efficient and flexible content management system for daily operations.AnQiCMS with its high-performance architecture based on the Go language and the friendly syntax of the Django template engine, provides us with great convenience.Today, let's explore a very practical technique in website frontend display: how to elegantly obtain the links, Logo images, and the number of documents under each category in the `categoryList` loop.

2025-11-06

How `tagList` tags display the related Tag list of the specified document?

As an experienced website operations expert, I know that how to flexibly and effectively organize and display content in a content management system is the key to improving user experience and search engine optimization (SEO).AnQiCMS (AnQi CMS) offers many powerful functions in this aspect, and the `tagList` tag is an important bridge to connect content and enhance discoverability.Today, let's delve into how to use the `tagList` tag in AnQiCMS to accurately display the associated tag list of a specified document.### Fine-grained content management

2025-11-06

How to retrieve all documents under a specific Tag and implement pagination using `tagDataList` tag?

In the vast field of content operation, tags (Tags) play a crucial role.They can not only thematize and structure scattered content, but also guide users to explore deeply, improve the efficiency of content discovery, and enhance the overall user experience of the website.As an experienced website operations expert, I am well aware of the powerful capabilities of AnqiCMS (Anqi CMS) in label management and content display.

2025-11-06