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