How does AnQiCMS display the list of all top-level categories?

Calendar 👁️ 57

In website construction and content operation, clear classification navigation is the foundation of user experience, as well as the core of content organization.For users of AnQiCMS, how to efficiently display the list of all top-level categories on the website is the first step in building the website structure.AnQiCMS with its flexible template engine and rich built-in tags makes this task exceptionally simple and intuitive.

Understanding the structure of categories and core tags

In AnQiCMS, content is organized according to the "content model" and "category".A category always belongs to a specific content model (such as an article model, product model, etc.), and categories can have a hierarchical relationship.Top-level category, as the name implies, it refers to those categories that do not have a parent category, they are the top level of the category hierarchy.

To display the list of top-level categories, we mainly use a core tag in the AnQiCMS template system:categoryList. This tag is used to retrieve and display category data and provides various parameters to accurately control the range of categories we want to retrieve.

How to display the list of all top-level categories

We will explain in detail the following steps on how to display the list of all top-level categories in the AnQiCMS template.

Firstly, we need to determine in the template file where you want to display these category lists. This is usually in the navigation bar, sidebar, or some area on the homepage, for exampleindex.html(Home page template) orpartial/header.html(Header common template) etc.

Next, we will usecategoryListtags to retrieve the required category data. This tag needs to be configured with two key parameters to accurately filter out all top-level categories:moduleIdandparentId.

  1. Confirm content model ID (moduleId)The classification of AnQiCMS is closely related to the content model.This means you need to tell the system which content model's top-level category you want to retrieve.moduleIdWith1If it is a "product" content model,moduleIdusually is2. You can view or edit the ID of each model in the 'Content Management' -> 'Content Model' section of the AnQiCMS backend.

  2. Specify the top-level category (parentId="0")In order to ensure that only the top-level categories are obtained, we need toparentIdthe parameter to0. In AnQiCMS,parentId="0"expressly indicate that the category has no parent category, i.e., it is a top-level category.

With these two key parameters, we can buildcategoryListtags. Here is an example of getting all top-level categories under the "Article" model:

{% categoryList categories with moduleId="1" parentId="0" %}
    {# 循环遍历获取到的顶级分类 #}
    {% for item in categories %}
        <li>
            <a href="{{ item.Link }}">{{ item.Title }}</a>
            {# 您也可以在这里添加其他分类信息,例如简介或缩略图 #}
            {% if item.Description %}<p>{{ item.Description }}</p>{% endif %}
            {% if item.Thumb %}<img src="{{ item.Thumb }}" alt="{{ item.Title }}"/>{% endif %}
        </li>
    {% empty %}
        <li>暂无顶级分类信息。</li>
    {% endfor %}
{% endcategoryList %}

In this code block:

  • {% categoryList categories with moduleId="1" parentId="0" %}This line of code will query all from the databasemoduleIdWith1(Article model) ANDparentIdWith0(Top category) classification data, and store the results in a namedcategories.
  • {% for item in categories %}: This is a loop structure, it will iterate through one by onecategoriesEach category in the variable. In each loop, the data of the current category will be assigned toitemVariable.
  • {{ item.Link }}: Displays the link address of the current category, clicking on it will jump to the list page of the category.
  • {{ item.Title }}Display the current category name.
  • {{ item.Description }}Display the current category description.
  • {{ item.Thumb }}Display the current category thumbnail.
  • {% empty %}If:categoriesNo data in the list.emptyContent inside the block (for example, “No top-level category information available.”) will be displayed.

In this way, you can flexibly display the top-level category list of the required content model at any location on the website.This tag-based calling method greatly simplifies template development, allowing you to focus more on website design and content presentation.

Advanced applications and practical skills

  • Display top-level categories of multiple content models:You can use tags separately to display the top-level categories of articles and products on a page.moduleIdWith1and2ofcategoryListAnd display them in different areas.
  • Determine whether the current category is selected:When building the navigation menu, you may want to highlight the category that the user is currently accessing.itemThere is a variable with oneIsCurrentfield, which is a boolean. You can use{% if item.IsCurrent %}Add a CSS class to the currently selected item to highlight it.
  • Modularize the code:It is recommended to keep the template file tidy and maintainable by placing this commonly used category list code snippet in a separate partial file, for examplepartial/top_categories.htmlUse where necessary{% include "partial/top_categories.html" %}For reference

Through AnQiCMS'scategoryListTags, it becomes easy to display the list of all top-level categories, greatly enhancing the flexibility of the website structure and the efficiency of content organization.

Frequently Asked Questions (FAQ)

  1. How to obtain the top-level category of a specific content model (such as 'product')?You just need to adjustmoduleIdthe value of the parameter. For example, to obtain the top-level category of the product model, you can setmoduleId="1"changed tomoduleId="2"(Please confirm the specific ID according to your backend settings).

  2. Can I control the display order of the top-level categories?Yes, you can edit each category in the 'Content Management' -> 'Document Category' of the AnQiCMS backend, where there is a 'Display Order' field.The lower the number, the earlier the category is displayed in the list.categoryListLabels will be displayed in this order by default.

  3. How to display the list of subcategories under the top-level category?To display subcategories, you can iterate over the top-level categories and setforit again within the loopcategoryListLabel, andparentIdthe current top-level category toitem.Id. At the same time,itemthe variable also has aHasChildrenThe field can determine whether the current category has subcategories, helping you decide whether to display it nested. For example:

    {% for item in categories %}
        <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
        {% if item.HasChildren %}
            <ul>
                {% categoryList subCategories with parentId=item.Id %}
                    {% for subItem in subCategories %}
                        <li><a href="{{ subItem.Link }}">{{ subItem.Title }}</a></li>
                    {% endfor %}
                {% endcategoryList %}
            </ul>
        {% endif %}
    {% endfor %}
    

Related articles

How to filter and display a specific list of articles based on recommended properties (Flag) in AnQiCMS?

An QiCMS provides operators with rich tools for organizing and displaying website content with its flexible and efficient content management capabilities.Among the "recommended attributes" (Flag) is a very practical feature that allows you to easily categorize and mark articles, thereby enabling accurate content filtering and dynamic display on the website front end.This article will introduce in detail how to use these recommended attributes in AnQiCMS to filter and display the specific article list you want.### Deep Understanding of AnQiCMS Recommendation Attributes (Flag) AnQiCMS Recommendation Attributes

2025-11-08

How to nested display the list of articles under the category page in AnQiCMS?

In website operation, the content classification page carries the important responsibility of organizing information and guiding users to browse.A well-designed and content-rich category page can not only improve user experience but also optimize search engine crawling efficiency.For users of AnQiCMS, nesting the list of articles under the category page is a very basic and commonly used feature.This article will introduce you in detail on how to use the powerful template tags of Anqi CMS to easily achieve this goal.Understanding the Classification Page and Content Model Before we begin

2025-11-08

How to judge and highlight the first article in the AnQiCMS article list loop?

In website content display, we often encounter the need to handle the first element of the article list specially.In order to achieve visual prominence, to implement unique layout design, or to give the first article more attention, AnQiCMS provides flexible and powerful template tags to help us easily achieve this goal.AnQiCMS uses a syntax similar to the Django template engine, where the `for` loop tag plays a core role in processing content lists.When we use `archiveList`

2025-11-08

How to display the reading views of articles on the AnQiCMS article list or detail page?

In website content operation, the number of article views is an important indicator, which helps us understand the popularity of the content and the user's interests.AnQiCMS provides a convenient way for you to visually display this data on the article list page or detail page. ### Understanding AnQiCMS's Page View Mechanism AnQiCMS is a comprehensive content management system that comes with a built-in mechanism for automatically recording article page views.

2025-11-08

How to display the subcategory list under the AnQiCMS category page?

In AnQiCMS, you can easily display the subcategory list under the category page with flexible template tags.This is crucial for building a clear website structure, improving user navigation experience, and optimizing search engine crawling efficiency.AnQiCMS powerful template system and its Django-style tag syntax make the display of such dynamic content intuitive and efficient.To implement displaying the subcategory list under the category page, the following steps are mainly involved:

2025-11-08

How to implement nested display of multi-level categories in AnQiCMS navigation menu?

In website construction, a clear and easy-to-use navigation menu is crucial for improving user experience and the overall structure of the website.AnQiCMS provides flexible settings for managing the navigation menu, allowing users to easily implement nested hierarchical classification display, thereby better organizing website content and guiding visitors to explore.To implement multi-level nested navigation, we mainly complete it through the "Navigation Settings" function in the AnQi CMS backend.This feature is located in the "Background Settings" area, it can not only manage the top navigation of the website, but also create various custom navigations as needed

2025-11-08

How to obtain and display the detailed description information of AnQiCMS categories?

In website operation, clear and detailed classification descriptions are crucial for user experience and search engine optimization.AnQiCMS as a powerful content management system provides a flexible way to manage and display these category information.This article will delve into how to retrieve and effectively display detailed category descriptions on the frontend page in AnQiCMS.

2025-11-08

How to call and display the Banner image of the AnQiCMS category page

In website operation, the category page is not only the display of content classification, but also a key entry for users to explore the website and gain a deeper understanding of products or services.A well-designed category banner image that can effectively enhance the visual appeal of the page, strengthen the brand image, and guide users to the next step of operation.AnQiCMS as an efficient content management system, provides a flexible way to manage and call these banner images. We will delve into how to set up a Banner image for category pages in AnQiCMS and display it on the website frontend.###

2025-11-08