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.