EnglishCMS: How to obtain and display all top-level category lists on a website page

Understand the classification system of AnQi CMS

In the Auto CMS, content is organized in the form of 'Categories'.Each category belongs to a specific "content model" (for example, article model, product model), which makes content management more flexible and structured.To get the list of categories of the website, we first need to understand how these categories are associated with the content model, because when calling category data, it often requires specifying the ID of the content model.

ApplicationcategoryListTag to get category data

The template system of EnglishCMS provides a namedcategoryListThe dedicated tag for obtaining and displaying the website's category data in a loop.This tag is designed to be very flexible, and can be configured according to different requirements (such as obtaining the classification of a specific model, obtaining the sub-classification, obtaining all classifications, etc.).categoryListThe key to the tag is itsparentIdParameter.

Accurately obtain the top-level category list

In order to accurately retrieve all top-level categories that do not belong to any parent category, we need toparentIdparameter settings"0"。Additionally, since each category in the Anqi CMS is bound to a specific content model, when retrieving the top-level categories, we also need to specify the content models they belong to.moduleId。For example, if we want to get all top-level categories under the article model (usuallymoduleId="1"),then the combined tags will specifymoduleId="1"andparentId="0"like this,categoryListThe label can precisely return the top-level classification data we need.

Display the top-level classification list on the page

Once we have passed throughcategoryListOnce the label gets the top-level classification data, it can go throughforLoop through these data and display them on the page. Inside the loop, we can easily access various properties of each category, such as the category's ID (item.Id) and the title (item.Title) and the link (item.Link) 等。以下是一个具体的模板代码示例,演示了如何获取文章模型(假设moduleIdUnder 1) all top-level categories are displayed in a list with their titles and links:

{# 假设要获取文章模型(moduleId="1")的所有顶级分类 #}
<nav class="main-category-navigation">
    <ul>
        {% categoryList categories with moduleId="1" parentId="0" %}
            {% for item in categories %}
                <li>
                    <a href="{{ item.Link }}">{{ item.Title }}</a>
                    {# 如果需要,可以根据 item.HasChildren 判断是否有子分类并决定是否加载更多内容 #}
                </li>
            {% empty %}
                <li>目前没有顶级分类可供显示。</li>
            {% endfor %}
        {% endcategoryList %}
    </ul>
</nav>

In this example,categoriesis the variable name we define for the top-level category list. Through{{ item.Link }}and{{ item.Title }}We have separately output the classification access address and display name.{% empty %}An auto is a very practical feature, when there is no category data returned, it will display a friendly prompt message to enhance the user experience.

Integrate the code into your website template

This code can be placed at different positions in the template according to the design requirements of the website. For example, if this is part of the website's main navigation, it may be included inpartial/header.htmlorbase.htmlThis public template file ensures consistency across all pages. If it is a sidebar category list, it can be placed inpartial/aside.htmlOr directly embedded into the page template that needs it, such asindex/index.html. No matter where it is placed,categoryListthe tag can ensure that the latest category data is dynamically loaded without manual update.

The importance of the content model ID

Ensure that in actual operation,moduleIdThe value matches the content model ID associated with the category you want to retrieve. Safe CMS allows users to customize content models, so different websites have different content models.moduleIdMay vary. You can check and manage the content model by selecting "Content Management" in the "Safety CMS" backend menu. Proper specificationmoduleIdIt is a key step to obtain an accurate classification list.

PasscategoryListlabel combined withmoduleIdandparentId="0"Parameters, Anqi CMS provides an efficient and flexible way to retrieve and display all top-level categories of the website.This method not only maintains the clarity and neatness of the website structure, but also greatly enhances the discoverability and user experience of the content.As an operations person, I know that the ease of use of tools is the key to improving efficiency, and Anqi CMS excels in this aspect.

Common Questions and Answers (FAQ)

1. How to know my content model?moduleIdWhat is it?

You can log in to the Anqi CMS backend, navigate to the 'Content Management' menu, and then select 'Content Model'.There, you will see all the defined content models and their corresponding IDs.For example, the 'article model' usually has an ID of 1, the 'product model' has an ID of 2, but custom models will have their specific IDs.Make sure to use the exact numeric ID displayed here.

2. How can I modify the code to display subcategories under the top-level category at the same time?

If you want to display subcategories under the top-level category, you can do so by modifying the top-level category'sforWithin the loop, use againcategoryListtags, andparentIdset the parameter to the current top-level category.item.Id. For example:{% categoryList subCategories with parentId=item.Id %}, then in this newsubCategoriesloop to display subcategories. This can build a multi-level navigation structure.

3. Why does my top-level category list not display any content?

This could have several reasons. First, please checkmoduleId