How to call the category list and display the number of articles under it in AnQiCMS template tags?

Calendar 👁️ 66

In a content management system, effectively organizing and displaying content is the key to improving user experience and website SEO performance.AnQiCMS provides powerful and flexible template tags, allowing developers to easily build richly functional page layouts.Among them, calling the category list and displaying the number of articles under it is a very common and practical demand in website navigation and content overview.This not only helps users quickly understand the amount of content in each category, but also provides clear website structure information for search engines.

Understand the core function of the `categoryList` tag

In the template system of AnQiCMS,categoryListTags are key tools for obtaining the list of website categories. Through this tag, we can flexibly extract the classification information we need according to different requirements.For example, if you want to get all the top-level categories of articles, or the child categories under a specific parent category,categoryListCan easily handle.

While usingcategoryListWhen labeling, you will find that it supports some important parameters to accurately locate the required classification:

  • moduleId: This is a very important parameter that allows you to specify which content model category to retrieve. For example, if you want to list all categories of the "article" type, you can setmoduleId="1"(Assuming the article model ID is 1). Similarly, product categories may correspondmoduleId="2". This ensures that only categories related to a specific content type are displayed.
  • parentId: This parameter is used to specify the ID of the parent category. When set toparentId="0"It will return all top-level categories. If you want to get the subcategories under a specific category, just use the ID of that category asparentIdthe value.

categoryListAfter the label is executed, it will return an array containing multiple categorized objects. We usually specify a variable name for this array, such ascategoriesThen loop through these category objects to display them.

Easily get the number of articles under the category: `ArchiveCount` field

The convenience of AnQiCMS template tags lies in the fact that when you usecategoryListWhen getting the category list, each returned category object containsa field namedArchiveCountdirectlyThis field does not require additional queries, it will automatically count and store the number of articles associated with this category (and its subcategories, ifchildThe parameter is set to default.trueso)

This means that you can directly access the category list while traversingitem.ArchiveCountTo retrieve and display the number of articles under each category without needing to perform complex secondary data processing. This greatly simplifies the development of templates and improves efficiency.

Practical Code Example: Displaying Category List and Article Count

Now, let's demonstrate how to implement this feature with specific code snippets.

Example 1: Display all top-level article categories and their number of articles

Assuming your website has multiple top-level article categories, you want to display these category names in the sidebar or main navigation and clearly indicate how many articles each category currently contains.

<nav class="category-navigation">
    <h3>探索我们的文章分类</h3>
    <ul class="category-list">
        {% categoryList categories with moduleId="1" parentId="0" %}
            {% for item in categories %}
                <li>
                    <a href="{{ item.Link }}" title="查看{{ item.Title }}下的所有文章">
                        {{ item.Title }}
                        <span class="article-count">({{ item.ArchiveCount }} 篇文章)</span>
                    </a>
                </li>
            {% empty %}
                <li>暂无分类内容。</li>
            {% endfor %}
        {% endcategoryList %}
    </ul>
</nav>

This code first usescategoryListtag to obtainmoduleIdis "1" (usually representing an article model) andparentIdAll categories for “0” (i.e., the top-level category). Then, it will iterate through these categories, generating a link for each category and displaying its title (item.Title) and the number of articles (item.ArchiveCount).item.LinkIt will automatically generate the access link for the category. If there are no categories,emptypart of the content will be displayed.

Example two: Display multi-level category structure and count the number of articles at each level

In some cases, your classification may have multi-level nesting.You may wish to display the parent category while also seeing its subcategories and showing the number of articles for each separately.AnQiCMS 'scategoryListLabels can also support this scenario very well.

<div class="multi-level-categories">
    <h2>内容导航</h2>
    {% categoryList topCategories with moduleId="1" parentId="0" %}
        <ul class="main-categories">
            {% for topItem in topCategories %}
                <li>
                    <a href="{{ topItem.Link }}">{{ topItem.Title }}</a> 
                    <span class="count">({{ topItem.ArchiveCount }} 篇文章)</span>
                    
                    {% if topItem.HasChildren %} {# 判断当前分类是否有子分类 #}
                        <ul class="sub-categories">
                            {% categoryList subCategories with parentId=topItem.Id %}
                                {% for subItem in subCategories %}
                                    <li>
                                        <a href="{{ subItem.Link }}">{{ subItem.Title }}</a> 
                                        <span class="count">({{ subItem.ArchiveCount }} 篇文章)</span>
                                        {# 如果还需要更深层级,可以继续嵌套 categoryList #}
                                    </li>
                                {% endfor %}
                            {% endcategoryList %}
                        </ul>
                    {% endif %}
                </li>
            {% endfor %}
        </ul>
    {% empty %}
        <p>没有找到任何分类。</p>
    {% endcategoryList %}
</div>

In this example, we first obtain the top-level category(topCategories). While traversing each top-level category, we utilizetopItem.HasChildrenThis boolean value is used to determine whether the category has subcategories. If it exists, we will use it againcategoryListTag, and use the ID of the current top-level category asparentIdto retrieve and display the sub-categoriessubCategoriesSimilarly, display the number of articles.

Content operation value consideration

In website operation, clearly displaying categories and their number of articles has multiple values:

  • Improve user experience:Users can intuitively see the richness of content in each category while browsing the website, which helps them quickly locate topics of interest and reduce the time spent on blind exploration.
  • Optimize content discovery:For new users or those visiting a specific page for the first time, the number of articles provides an expectation of content depth, encouraging them to delve deeper.
  • Assist SEO strategy:A clear categorization structure and content quantity suggest the breadth and depth of the website's content, which is very beneficial for search engines to understand the website's theme and crawl and index.At the same time, the number of articles on the category page can be used as a reference for page weight distribution.
  • Guide content planning:The operation team can judge which categories have relatively weak content based on the number of articles in each category, and then supplement and optimize the content in a targeted manner.

AnQiCMS template tag design fully considers these operational needs, simplifying complex data calls into intuitive and easy-to-use tags, making the organization and display of website content unprecedentedly convenient. By flexibly usingcategoryListandArchiveCountTags, you can provide users with a better browsing experience and lay a solid foundation for the long-term development of the website.


Frequently Asked Questions (FAQ)

1. If there are no articles under a certain category,ArchiveCountwhat will be displayed?

When there are no related articles under a certain category,ArchiveCountthe field will naturally display as0This means you do not need to make additional conditional judgments to handle the case of empty categories, the template will directly output zero, which is also very clear to users.

2. In addition to the number of articles,categoryListWhat information can the tag obtain about the category?

categoryListEach category object returned by the tag is very rich. In addition toId/Title/LinkandArchiveCount, you can also accessDescription(Category Introduction),ParentId(Parent Category ID),Logo(Category Large Image),Thumb(Category Thumbnail),HasChildren(Whether there are subcategories) and various information. This gives you a high degree of freedom when designing category displays.

3. Can I get the categories under a specific content model without affecting other models?

Of course you can.categoryListlabel'smoduleIdThe parameter is set for this purpose. By settingmoduleId="您内容模型的ID"You can precisely specify to only retrieve category lists under articles, products, or other custom content models without confusing different types of content. For example, if you want to display product categories only on a certain page, you can setmoduleIdSet the ID of the product model.

Related articles

How to use AnQiCMS's flexible content model to achieve personalized field display on product detail pages?

## Utilizing AnQiCMS flexible content model, create uniquely crafted product detail pages When operating a website, we all hope that product detail pages can attract visitors and provide clear, personalized product information.However, when facing different types of products, using a fixed template alone often fails to fully showcase their unique features, and may even make visitors feel that the information is insufficient or confusing.For example, the display parameters required for a mobile phone and a T-shirt are completely different, forcing a template will only make things twice as difficult.

2025-11-08

How to optimize the SEO-friendly URL of the article detail page through the pseudo-static function of AnQiCMS?

A clear, readable, and relevant URL address is crucial for a website's performance in search engines.It can not only help users understand the page content intuitively, but also assist search engines in accurately understanding and indexing your page.In AnQiCMS, to implement URL optimization for the article detail page, the static function is undoubtedly our helpful assistant.What is a static URL and why is it important?

2025-11-08

How to implement differentiated display of content for PC and mobile terminals in AnQiCMS templates?

In today's internet environment, providing a high-quality cross-device browsing experience is the key to the success of website operations, whether it is a corporate website, e-commerce platform, or personal blog.AnQiCMS (AnQi CMS) has fully considered this requirement in template design, providing users with flexible and diverse solutions to achieve differentiated display of content on PC and mobile endpoints.This is not just about adapting the interface layout, but also about how to present the most suitable content for users based on device characteristics.Next, we will delve into how the AnQiCMS template cleverly achieves this goal.

2025-11-08

How to enable Markdown rendering and correctly display mathematical formulas in article content?

AnQi CMS article content: Turn on Markdown rendering, easily handle mathematical formulas and flowcharts AnQi CMS is an efficient, customizable and easy-to-expand content management system, committed to providing users with a convenient content publishing and management experience.In daily content creation, we often encounter scenarios where it is necessary to insert code, tables, even complex mathematical formulas and flowcharts.Traditional rich text editors often fall short when handling this content, but Markdown, with its concise syntax and powerful expressiveness, has become the preferred choice for many content creators.It is fortunate that

2025-11-08

How to display the title and link of the previous and next articles on the article detail page?

In website content operation, improving user experience and content browsing depth is indispensable.After a visitor finishes reading an article, if they can easily browse to the titles and links of the "previous" and "next" articles, it will undoubtedly greatly encourage them to continue exploring the website's content, thereby extending their stay time, reducing the bounce rate, and even having a positive impact on the overall SEO performance of the website.AnQi CMS is an efficient and customizable content management system that fully considers this user requirement.A safe CMS to simplify the implementation of this common website feature provides intuitive and powerful template tags

2025-11-08

How to set and display the website filing number and copyright information at the bottom of the AnQiCMS page?

The website's filing number and copyright information are important elements that demonstrate the compliance and professionalism of the website. They usually appear in the footer, providing visitors with a sense of trust.In AnQiCMS, setting and displaying this information is simple and efficient.We will guide you through this process, ensuring that the key content is clearly presented at the bottom of your website. ### Step 1: Set the record number and copyright information in the AnQiCMS background First, log in to your AnQiCMS background management interface. This is the starting point of the basic information configuration for all websites. 1. **Navigate to the global feature settings:**

2025-11-08

How to safely output article content containing HTML tags in AnQiCMS templates (to prevent XSS)?

When processing article content that contains HTML tags in AnQiCMS templates, securely outputting is a crucial issue, as it directly concerns the safety and user experience of the website.Especially when the content of the article may be input by the background rich text editor, or when comments and messages submitted by users contain custom HTML, improper handling is likely to be attacked by cross-site scripting (XSS).AnQiCMS as a content management system that focuses on security and efficiency, provides the corresponding mechanisms to help us meet this challenge.Understanding Risk

2025-11-08

How to enable automatic image compression and WebP format conversion in AnQiCMS to improve page loading speed?

In today's fast-paced online environment, website loading speed has become a key indicator of user experience and search engine rankings.If your website has many images, they may be slowing down your website speed quietly.Fortunately, AnQiCMS fully considers this point and provides us with automatic image compression and WebP format conversion functions, which can effectively accelerate page loading and make your website fly.Why is image optimization so important?\n\nImagine when a user visits a page, if the images take a long time to display or load slowly

2025-11-08