How to call the article or product category list in AnQiCMS template and implement multi-level nesting?

Calendar 👁️ 54

As a senior website operator for AnQiCMS, I know that efficient content organization and presentation are crucial for user experience and SEO.Categorization list, especially multi-level nested categorization lists, can clearly present the hierarchical structure of the website content, guide users to find more interesting content, and also provide clear site structure information for search engines.This article will detailedly explain how to call the article or product category list in the AnQiCMS template and achieve multi-level nested display effects.

Understanding AnQiCMS template basics and classification logic

AnQiCMS uses a syntax similar to the Django template engine, making template development and customization intuitive and easy to learn.In AnQiCMS, content (such as articles, products) is closely associated with the 'content model', and categories further organize these contents.Each category belongs to and only belongs to one content model, which means that article categories and product categories are independent of each other.When calling the category list in the template, we need to specify the content model ID it belongs to, for example, the article model is usually ID 1, and the product model is usually ID 2.

AnQiCMS template files are usually stored in/templatedirectory, through structured tags (with{%and%}Wrap) for logical control, variables use double curly braces{{}}Outputting. This design allows us to flexibly build complex classified navigation structures through nested loops and conditional judgments.

Invoke the basic category list.

To display the most basic classification list on a page, such as the top navigation menu of a website, we can usecategoryListThe tag allows us to filter and retrieve category data based on model ID and parent category ID.

For example, if you want to get the list of all top-level article categories, you can use the following code:

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

In this code block:

  • categoryListIt is a label for the category list,categoriesIt is the variable name defined for the category data we obtain.
  • moduleId="1"Specified that we need to retrieve the category of the article model (ID 1).
  • parentId="0"Means that we only retrieve top-level categories without parent categories.
  • forLoop throughcategoriesEach category item in the variable.
  • item.Linkanditem.TitleUsed to output the links and titles of categories.

In this way, you can easily build the main navigation menu of a website or the top-level category display of a certain content area.

Implement hierarchical category nesting

Hierarchical category nesting is the key to building complex navigation and content indexing. AnQiCMS'scategoryListTag throughparentIdParameters and categories自带HasChildrenProperties that make it very convenient to implement multi-level nesting. We can call another category loop inside.categoryListTag to get the subcategories of the current category.

Here is a typical code example for implementing a three-level category nesting:

{% categoryList categories with moduleId="1" parentId="0" %}
{# 一级分类开始 #}
<ul>
    {% for item in categories %}
    <li>
        <a href="{{ item.Link }}">{{item.Title}}</a>
        {# 判断当前一级分类是否有子分类 #}
        {% if item.HasChildren %}
        {# 二级分类开始:调用当前一级分类的子分类 #}
        <ul>
            {% categoryList subCategories with parentId=item.Id %}
            {% for inner1 in subCategories %}
            <li>
                <a href="{{ inner1.Link }}">{{inner1.Title}}</a>
                {# 判断当前二级分类是否有子分类 #}
                {% if inner1.HasChildren %}
                {# 三级分类开始:调用当前二级分类的子分类 #}
                <ul>
                    {% categoryList subCategories2 with parentId=inner1.Id %}
                    {% for inner2 in subCategories2 %}
                    <li>
                        <a href="{{ inner2.Link }}">{{inner2.Title}}</a>
                    </li>
                    {% endfor %}
                </ul>
                {% endcategoryList %}
                {# 三级分类结束 #}
                {% endif %}
            </li>
            {% endfor %}
        </ul>
        {% endcategoryList %}
        {# 二级分类结束 #}
        {% endif %}
    </li>
    {% endfor %}
</ul>
{# 一级分类结束 #}
{% endcategoryList %}

In this example:

  • The outermostcategoryListGet the top-level category (parentId="0").
  • in the firstforloop, we use{% if item.HasChildren %}to determine if the current category has subcategories.
  • If there is a subcategory, it will call againcategoryListThis time willparentIdSet to the current loop item'sitem.IdTo get its direct subcategories. We will store these subcategories insubCategoriesthe variable.
  • This pattern can be followed, by continuously nestingcategoryListandforLoop, cooperateHasChildrenJudge, to achieve classification nesting of any depth

Integration display of classification with article/product content

In addition to simply displaying the classification structure, you may also need to display related articles or product lists directly under the classification, or display content when there are no sub-classifications. This can be achieved by combiningarchiveListthe tag.

For example, in a product category navigation, if a category has subcategories, we want to display its subcategories; if it does not have subcategories, we want to display the product list under the category:

<div>
    {% categoryList productCategories with moduleId="2" parentId="0" %}
    {% for item in productCategories %}
    <a href="{{item.Link}}">{{item.Title}}</a>
    <ul class="ind-pro-nav-ul">
        {% if item.HasChildren %}
            {# 如果有子分类,显示子分类列表 #}
            {% categoryList subCategories with parentId=item.Id %}
            {% for inner in subCategories %}
            <li><a href="{{inner.Link}}" title="">{{inner.Title}}</a></li>
            {% endfor %}
            {% endcategoryList %}
        {% else %}
            {# 如果没有子分类,显示当前分类下的产品列表 #}
            {% archiveList products with type="list" categoryId=item.Id limit="8" %}
            {% for inner in products %}
            <li><a href="{{inner.Link}}" title="">{{inner.Title}}</a></li>
            {% endfor %}
            {% endarchiveList %}
        {% endif %}
    </ul>
    {% endfor %}
    {% endcategoryList %}
</div>

This example shows how toHasChildrenThe boolean value of the attribute, dynamically renders subcategories in the category navigation or directly displays the content under the category.This is very useful for building a flexible product catalog or article theme, providing an experience tailored to the actual organization of the content.

optimize and **practice

Consider the following points when building a category list to further optimize the performance and maintainability of the website:

  • Style separation: Template code focuses on data logic and structure, while the visual style of lists and navigation should be controlled through external CSS files. This helps to maintain the cleanliness and readability of the template code.
  • The precise use of model IDAlways specify clearlymoduleIdTo ensure that you obtain the classification under the required content model (article or product), avoiding confusion or incorrect data calls.
  • Use public code snippets: For complex, repeatedly used classification list structures, consider encapsulating them as public code snippets (partial), by using{% include "partial/your_category_list.html" %}Make the call. This improves code reusability and simplifies the template structure.
  • Control the number of displays.: Use.limitParameter limits the number of categories or articles called each time, especially on the homepage or other pages with high load. This helps to improve page loading speed and optimize user experience.

By proficiently using AnQiCMS providedcategoryListandarchiveListLabels, combined with its flexible template syntax, you can build a powerful and user-friendly classification navigation system for your website, thereby effectively managing and displaying your content.


Frequently Asked Questions (FAQ)

How can I ensure that I only get the top-level categories and not all categories?You can specify only the top-level categories by using thecategoryListSet in the labelparentId="0"parameter. For example:{% categoryList categories with moduleId="1" parentId="0" %}.

2. Why is my category list not displaying subcategories, even though I have set it in the background?Make sure you use it internally when looping through the parent categories.{% if item.HasChildren %}Make a judgment and call it again if the condition is metcategoryListAnd take itparentIdSet the parameter to the current parent categoryId. For example:{% categoryList subCategories with parentId=item.Id %}. If the amount of child category data is large, it may also be necessary to check whether it is setlimitThe parameter caused the content to not be displayed completely.

3. Can I, in a loop of a category list, judge whether there are subcategories and whether there are articles/products?Yes, you can.forin a loopitem.HasChildrenTo determine if there is a subcategory. Ifitem.HasChildrenWithtruethen the subcategory list can be called; if it isfalse, then

Related articles

How to get and display the navigation list and its sub-menu in AnQiCMS template?

## AnQiCMS Navigation List and Submenu Display in Templates As an experienced AnQiCMS website operations personnel, I am well aware of the importance of a clear and efficient website navigation for user experience and content access.AnQi CMS provides a powerful and flexible navigation list tag `navList` for template developers, making it easy to obtain and display the main navigation, footer navigation, and even more complex multi-level navigation structures with submenus on the website front-end.

2025-11-06

How to call the TDK (Title, Keywords, Description) information of the page in AnQiCMS template?

As a website operator who is well-versed in the operation of AnQiCMS, I deeply understand the importance of TDK (Title, Keywords, Description) information for the performance of the website in search engines and the willingness of users to click.AnQiCMS was designed with SEO-friendliness in mind from the beginning, providing us with flexible and powerful mechanisms to manage these key metadata.Properly calling TDK in the template is the basis for ensuring that website content is efficiently indexed, improves click-through rates, and enhances user experience.

2025-11-06

How to get contact information in AnQiCMS template (such as phone, email)?

As a website operator who has been deeply involved in AnQiCMS for many years, I know the importance of clearly and effectively displaying key information, especially contact information, on the website front-end template.This is not only about user experience, but also the key to building trust and promoting communication.AnQiCMS was designed with this in mind from the beginning, providing intuitive and flexible template tags that allow developers to easily access and display this information.Next, I will elaborate on how to obtain and display contact information in the AnQiCMS template, such as phone and email.

2025-11-06

How to get system configuration information (such as website name, Logo) in AnQiCMS template?

As an expert in the long-term operation of AnQiCMS content, I fully understand the importance of dynamically obtaining system configuration information in the template.Efficiently display the website name, logo, and other core elements, which not only ensures brand consistency but also can flexibly adapt to changes in operational strategies.AnQiCMS's powerful template engine provides intuitive and feature-rich tags, making it easy to obtain these system-level configurations in front-end templates.

2025-11-06

How to obtain document detail data and display content in AnQiCMS template?

As an experienced security CMS website operator, I know that it is crucial to efficiently and accurately obtain and present document detail data in terms of content display.A well-crafted detail page not only meets users' in-depth information needs, but also effectively enhances the website's stickiness and search engine performance through optimizing content structure and user experience.In Anqi CMS, taking advantage of its flexible Django template engine syntax, obtaining and displaying document detail data is a direct and powerful process.

2025-11-06

How to call the previous and next document links in AnQiCMS template?

As a senior CMS website operation personnel in an enterprise, I am well aware of the importance of content navigation for user experience and SEO.On a day when the content of the website is becoming richer and richer, how to guide users to smoothly browse related content is a key factor in enhancing user stickiness and reducing the bounce rate.Today, we will delve into how to cleverly call the links of the previous and next documents in the AnQiCMS template to optimize your website's content structure and user path.### Enhancing User Experience and SEO: Navigation of Previous and Next Documents in AnQiCMS Templates In the Content Management System

2025-11-06

How to display the list of related documents in AnQiCMS template?

As a website operator who has been deeply involved in the CMS of security enterprises for many years, I know that effectively guiding users to discover more interesting content is crucial for enhancing user stickiness and the depth of website visits.Among them, the 'related document list' is undoubtedly one of the core functions to achieve this goal.It not only helps users conveniently explore more related topics, but also has a positive impact on the internal link structure of the website and SEO optimization.

2025-11-06

How to use the for loop in AnQiCMS template to traverse data and handle empty results with the empty tag?

## Master the data traversal and empty result handling in AnQiCMS templates: in-depth analysis of for loops and empty tags AnQiCMS, with its efficient and customizable features, has become the preferred content management system for many content operation teams and small and medium-sized enterprises.It is crucial for the flexibility of template language when building dynamic web pages.

2025-11-06