How to use the `categoryList` tag to get and loop through the category list under a specified content model?

Calendar 👁️ 79

As an expert who has been deeply engaged in the content operation of AnQiCMS for a long time, I fully understand the importance of the classification list in website structure and user experience. Flexible usecategoryListTags can help us efficiently organize content, build a clear navigation system, and provide readers with a more accurate path to content discovery. Below, I will elaborate on how to usecategoryListTag to get and loop to display the category list under the specified content model.

Using AnQiCMS'scategoryListTag to get and loop to display the category list under the specified content model.

In AnQiCMS template development,categoryListThe tag is used to obtain the core tool for website category data. It allows us to accurately extract and display category information according to different needs, such as content model, hierarchical relationships, etc.This is crucial for building dynamic navigation, category list page sidebar, content filter, and other functions.

UnderstandingcategoryListThe core function of the tag

categoryListThe main function of the tag is to retrieve category data that meets specific conditions from the database.It supports various parameters to finely control the range and method of data acquisition and returns the result in a recyclable object form for easy rendering in templates.AnQiCMS's template engine syntax is similar to Django, therefore it is used incategoryListWe usually combine{% for ... in ... %}Loop tags to traverse the obtained category data

Key parameter analysis: specify content model and hierarchical control

To implement obtaining the category list under the specified content model,moduleIdthe parameter is the core. At the same time,parentIdthe parameter is used to control the hierarchical relationship of categories, helping us build multi-level category navigation.

moduleId: Specify the unique identifier of the content model

moduleIdThe parameter is used to filter out categories that belong to a specific content model. In AnQiCMS, a content model is the foundation of our custom content structure, such as the "article" model, "product" model, and so on.Each content model has a unique ID.

  • Example of usage: moduleId="1"ormoduleId="2". If the "Article" model ID is 1, and the "Product" model ID is 2, thenmoduleId="1"only the article category will be retrieved, whereasmoduleId="2"only the product category will be retrieved.
  • Application scenario:When we need to display different content model categories in different areas of the website (such as displaying article categories in the sidebar of the article list page, and product categories in the sidebar of the product list page), this parameter is essential.

parentId: Controls the hierarchy of categories

parentIdThe parameter is used to control the level of the category we want to retrieve.

  • To get the top-level category:settingparentId="0"Get all the top-level categories under the content model. This is very useful when we want to build the main navigation menu.
  • Get the subcategories:When traversing the parent category in a loop, we can use the current parent category'sIdas the child category'sparentIdto achieve nested display of multi-level categories.
  • Get the sibling category:toparentIdis set to"parent"(Valid only on the current page as a category list page), can obtain the sibling categories of the current category.

limit: Control the display quantity and offset

limitThe parameter is used to limit the number of categories returned. It supports two modes:

  • Fixed number: limit="10"It means that up to 10 categories can be displayed.
  • Offset mode: limit="2,10"Starting from the second record, retrieve 10 categories of data. This is very useful when skipping a few records is required in some special layouts.

Other auxiliary parameters

  • allGet all categories at all levelsWhenall=truethen,categoryListIt will get the specifiedmoduleIdAll categories under it, regardless of their level. This is very useful when it is necessary to display all categories in full without concerning the hierarchical structure.
  • siteId: Multi-site data callFor users who use the AnQiCMS multi-site management function,siteIdThe parameter allows us to specify which site to retrieve category data from. Generally, if only managing one site, this parameter does not need to be filled in.

Loop traversal and data access

categoryListTags return a iterable collection of objects (usually namedcategoriesor a variable name you define). We use{% for ... in ... %}Loop to access each category item. Within the loop, each category item is usually nameditem, and provides various fields for us to call.

itemCommon fields of the object:

  • Id:Category ID, unique identifier.
  • Title:Category name, used for display on the front end.
  • Link:URL link corresponding to the category, for easy navigation.
  • Description:Category overview or description.
  • ParentId:ID of the parent category.
  • Logo/Thumb:Category large image or thumbnail address, which can be used for category navigation icons or backgrounds.
  • Spacer:Prefix used to display hierarchical indentation (e.g.):--配合 multi-level category display effect.}
  • HasChildren:Boolean value indicating whether the current category has subcategories. This is very useful when building dynamic menus.
  • IsCurrent:Boolean value indicating whether the current category is the category of the current page. It can be used to highlight the current navigation item.
  • ArchiveCount:The number of documents included in the current category.

Practical Application Examples

1. Retrieve all top-level categories under the "Article" model

The following code retrieves all top-level categories under the content model with ID 1 (assuming it is an "article" model) and prints out their names and links.

<nav class="main-categories">
    <h3>文章分类</h3>
    <ul>
        {% categoryList articleCategories with moduleId="1" parentId="0" %}
            {% for item in articleCategories %}
                <li {% if item.IsCurrent %}class="active"{% endif %}>
                    <a href="{{ item.Link }}">{{ item.Title }}</a>
                </li>
            {% endfor %}
        {% endcategoryList %}
    </ul>
</nav>

2. Implement multi-level category navigation (taking the 'Product' model as an example)

This example shows how to build a two-level navigation where the top-level categories come from the 'Product' model (assumingmoduleId="2"),and display its direct subcategories below.

<ul class="product-menu">
    {% categoryList productRootCategories with moduleId="2" parentId="0" %}
        {% for rootCategory in productRootCategories %}
            <li {% if rootCategory.IsCurrent %}class="active"{% endif %}>
                <a href="{{ rootCategory.Link }}">{{ rootCategory.Title }}</a>
                {% if rootCategory.HasChildren %}
                    <ul class="submenu">
                        {% categoryList productSubCategories with parentId=rootCategory.Id %}
                            {% for subCategory in productSubCategories %}
                                <li {% if subCategory.IsCurrent %}class="active"{% endif %}>
                                    <a href="{{ subCategory.Link }}">{{ subCategory.Title }}</a>
                                </li>
                            {% endfor %}
                        {% endcategoryList %}
                    </ul>
                {% endif %}
            </li>
        {% endfor %}
    {% endcategoryList %}
</ul>

3. Combine the document list to display the content under the category.

This scenario is common on the homepage or specific column pages, and it needs to display each category and the latest documents under the category. Suppose we have an article model(moduleId="1")

<section class="latest-articles">
    <h2>最新文章</h2>
    {% categoryList mainArticleCategories with moduleId="1" parentId="0" %}
        {% for category in mainArticleCategories %}
            <div class="category-block">
                <h3><a href="{{ category.Link }}">{{ category.Title }}</a></h3>
                <ul class="article-list">
                    {% archiveList articlesInCategory with type="list" categoryId=category.Id limit="5" %}
                        {% for article in articlesInCategory %}
                            <li>
                                <a href="{{ article.Link }}">
                                    {% if article.Thumb %}<img src="{{ article.Thumb }}" alt="{{ article.Title }}" />{% endif %}
                                    <h4>{{ article.Title }}</h4>
                                    <p>{{ article.Description|truncatechars:80 }}</p>
                                    <span class="date">{{ stampToDate(article.CreatedTime, "2006-01-02") }}</span>
                                </a>
                            </li>
                        {% endfor %}
                    {% else %}
                        <li>该分类下暂无文章。</li>
                    {% endarchiveList %}
                </ul>
            </div>
        {% endfor %}
    {% endcategoryList %}
</section>

Summary

categoryListThe label is a powerful and flexible tool in the AnQiCMS template system. It allows for precise

Related articles

How to implement dynamic breadcrumb navigation in AnQiCMS template and customize the home page text?

As a senior security CMS website operator, I know that high-quality content and excellent user experience are the foundation of website success.With the powerful functions of AnQiCMS, we can not only efficiently manage content, but also provide users with a smooth and intuitive browsing experience through fine-tuned template customization.Today, let's delve deeply into how to implement dynamic breadcrumb navigation in the AnQiCMS template, and flexibly customize the home page text to enhance the usability and search engine friendliness of the website.

2025-11-06

How to dynamically generate the SEO title, keywords, and description of the current page and support the website name suffix?

As an experienced content management expert who is deeply familiar with AnQiCMS operations, I know the importance of Search Engine Optimization (SEO) for website traffic and user retention.The website's SEO title, keywords, and description (usually abbreviated as TDK) are key factors for search engines to understand page content and for users to decide whether to click.AnQiCMS provides a powerful and flexible mechanism that allows us to dynamically generate these important SEO elements and conveniently integrate the website name as a suffix to enhance brand recognition.

2025-11-06

How to call and display the contact information (such as phone, email, social media) configured in the AnQiCMS template?

As a senior content worker who deeply understands the operation of Anqi CMS, I know the importance of displaying the contact information of the enterprise externally.This not only concerns whether customers can conveniently contact you, but also is the basis for building trust and promoting business cooperation.AnQi CMS provides a直观 and flexible solution for website operators, allowing you to easily manage and display various contact information without writing complex code.

2025-11-06

How to get and display the global configuration information (such as name, Logo) of AnQiCMS template?

As an experienced website operator proficient in AnQiCMS, I know that efficiently and accurately obtaining and displaying the global configuration information of the website is crucial for website maintenance, brand consistency, and user experience.AnQiCMS provides intuitive and powerful template tags, making it easy to achieve this goal.In AnQiCMS template, obtain and display the global configuration information of the website, such as the website name, Logo, filing number, etc., mainly depending on the built-in `system` tag

2025-11-06

How to get the details of the current access category, including the title, link, description, and custom fields?

As an experienced CMS website operator, I am well aware of the importance of high-quality and easily accessible content information for website user experience and search engine optimization.In Anqi CMS, obtaining detailed information about the current visited category is a very basic and frequent requirement in website construction. Whether it is used for page titles, navigation breadcrumbs, or detailed introductions on category pages, it is necessary to accurately call these data.Our AnQi CMS provides a powerful and flexible template tag system, making it extremely easy to obtain the title, link, description, and even custom fields of the current visited category

2025-11-06

How to get the list of all single pages or the detailed content of a specific single page in AnQiCMS template?

As a senior security CMS website operator, I am well aware of the importance of high-quality, well-structured content for website user experience and search engine optimization.A single page is the core of a website's basic information display, such as "About Us", "Contact Information", and "Terms of Service", etc.In AnQiCMS, through the powerful template tag system, we can flexibly obtain and display the content of these single pages. I will elaborate in detail on how to obtain the list of all single pages and the detailed content of a specific single page in the AnQiCMS template.###

2025-11-06

How to use the `archiveList` tag to get the article list and support sorting by ID, views, or custom sorting?

As an experienced CMS website operation person, I know that efficient content management is crucial for attracting and retaining users.The template tag system provided by AnQi CMS is its strong point, among which the `archiveList` tag is the core of daily content display.It can help us flexibly extract articles, products, and other content from the database and sort and display them in the way we want.

2025-11-06

How to implement complete pagination functionality for the article list in AnQiCMS template?

As an expert deeply familiar with the operation of AnQiCMS, I know that a smooth, user-friendly website experience is crucial for attracting and retaining users.The pagination feature of a content list is the cornerstone of any content-intensive website, not only optimizing the user's browsing experience, but also an important aspect of search engine optimization.In AnQiCMS, achieving complete pagination for the article list is efficient and flexible through its powerful template tag system.

2025-11-06