How to display the number of documents under each category on the category list page?

Calendar 👁️ 60

As an experienced website operations expert, I am well aware that how to efficiently display data in content management is crucial for user experience and the overall performance of the website.AnQiCMS (AnQiCMS) with its flexible and powerful template engine, allows us to easily achieve these seemingly complex requirements.Today, let's talk about how to clearly show the number of documents under each category on the category list page, which not only makes it easy for visitors to understand at a glance but also can improve the website's SEO performance in a certain degree.

Anqi CMS: Use template tags cleverly to display the number of documents on the category list page

In website operation, the category list page is an important entry for visitors to explore the website content.If the number of documents included under each category could be displayed intuitively next to the name of the category, it would undoubtedly greatly improve the user experience: visitors could find categories with a large amount of content they are interested in faster, and operators could also optimize content strategies through data feedback.AnQi CMS provides a very convenient way to implement this feature, thanks to its Django-style template syntax and rich built-in tags.

Understand the template basics of Anqi CMS

The AnQi CMS template system uses a syntax similar to Django, which means we can control page logic (such as loops, conditional judgments) and output variables through specific tags. The core idea is:{% ... %}Used to perform operations (such asforloop),{{ ... }}Used to output the value of a variable. Mastering this, we can easily master template customization.

Core tool:categoryListwith the tag andArchiveCountproperty

To display all categories and their document counts on a page, we first need to retrieve the category list. Anqicms providescategoryListThe tag, which is specifically used to pull category data. Even better, incategoryListeach category object looped out by the tag (usually nameditem), there is a built-in ArchiveCountThe property directly stores the total number of documents under the category.

This means that you do not need to perform complex database queries or additional calculations, just a simple call.item.ArchiveCountIt can get the number of documents in the current category.

Here is a basic code example that shows how to usecategoryListtag to get the category and display the number of documents:

{% categoryList categories with moduleId="1" parentId="0" %}
    <ul class="category-list">
        {% for item in categories %}
            <li class="category-item">
                <a href="{{ item.Link }}">
                    {{ item.Title }} ({{ item.ArchiveCount }} 篇文章)
                </a>
            </li>
        {% endfor %}
    </ul>
{% endcategoryList %}

In this code block:

  • {% categoryList categories with moduleId="1" parentId="0" %}We calledcategoryListLabel, assign the obtained category list tocategoriesVariable.moduleId="1"usually specifies the article model (it may differ according to your backend configuration),parentId="0"which means we want to retrieve all top-level categories.
  • {% for item in categories %}: We traversecategorieseach category in the list, and name the current category objectitem.
  • {{ item.Title }} ({{ item.ArchiveCount }} 篇文章): This is the key to implementing the core function.item.TitleOutput the category name, whileitem.ArchiveCountThe number of documents under the category was output, and we further explained it with parentheses and 'articles' to make the display more friendly.

Specific operation steps

  1. Locate the template file:Generally, the template file corresponding to the category list page varies according to your website design. If you are customizing the category list page of a content model, such as the list page of an article model, then the template file may be locatedtemplate/您的模板目录/{模型table}/list.htmlor likedesign-director.mdmentioned, using a flattened mode{模型table}_list.html. You can also view the specific 'category template' name used for each category in the 'Document Classification' settings.

  2. Insert label code:Find the part responsible for rendering the category list, which is usually a loop structure.Embed the given example code snippet cleverly into the display position of each category according to your HTML structure and style requirements.<li>Inside the label,<a>Beside the label, or anywhere you find suitable.

  3. Adjust display as needed:

    • Handle zero document classification:Sometimes there may not be any documents under a category. You can choose to display "Category Name (0 articles)" or use conditional judgment{% if item.ArchiveCount > 0 %}Decide whether to display the document count, or show a different prompt when the count is zero.
      
      <a href="{{ item.Link }}">
          {{ item.Title }}
          {% if item.ArchiveCount > 0 %}
              ({{ item.ArchiveCount }} 篇文章)
          {% else %}
              (暂无文档)
          {% endif %}
      </a>
      
    • Stylize:Use CSS to add styles to the part displaying the number of documents, making it stand out or better integrate with the overall design.

More flexible application and thinking

  • Home page or other pages:This method of displaying the number of documents is not limited to the category list page. You can call it from anywhere on the website, such as the homepage, sidebar, or footer.categoryListtags, and specifymoduleIdandparentIdto display any category and its document count.
  • Details of a single category:If you only need to geta singlecategory (not a list) document count, you can usecategoryDetailLabel, and throughidortokenGet the category specified by the parameter, and then obtainArchiveCountProperty.
    
    {% categoryDetail currentCategory with id="10" %}
        当前分类 {{ currentCategory.Title }} 下有 {{ currentCategory.ArchiveCount }} 篇文章。
    {% endcategoryDetail %}
    

This design concept of AnQi CMS greatly simplifies the complexity of content presentation. ThroughcategoryListBuilt-in tagsArchiveCountProperty, you can easily implement the display of the number of documents on the category list page, providing users with clearer and more valuable navigation information.


Frequently Asked Questions (FAQ)

  1. Why does the document count show 0 after I operate according to the above method, but I clearly have documents?If it shows 0, please check firstcategoryListin the tagmoduleIdThe parameter is correct, it should correspond to the actual content model ID you are using (for example, articles are usually 1, products might be 2, etc.).Secondly, please confirm that these documents have been published and are in normal status (not draft, not recycled), and that they indeed belong to the categories of the number of documents you are displaying.Finally, confirm that the document's category ID matches the one you expect.

  2. Ask: Can I display the number of documents under the category on the homepage or sidebar of the website?Answer: Absolutely.categoryListTags are universal, you can use them in any template file on the website (includingindex.htmlor the sidebar'spartial/sidebar.htmluse it in the following. Just follow the method described in the article, throughmoduleIdandparentIdparameters to get the category list you want, and then display it in a loopitem.Titleanditem.ArchiveCountJust do it.

  3. Question:ArchiveCountThe document count is whether it only includes documents under the current category, or whether it will recursively count all sub-category documents?Answer: Anqi CMS'ArchiveCountThe attribute is usually a countDirectly attributed to the current categorythe document count, excluding documents in its subcategories.If you need to count the total number of documents under a category and all its subcategories, this may require more complex logic, such as recursive queries at the controller layer, or using more advanced aggregation tags that AnQiCMS might provide in the future.But for most scenarios, simply counting the number of documents in the current category is sufficient.

Related articles

How to implement custom parameter filtering for document lists in AnQiCMS (such as by region, type)?

## Unlock content potential: How AnQiCMS implements custom parameter filtering of document lists (such as by region, type) As an experienced website operations expert, I am well aware of the importance of an efficient content management system (CMS) in enhancing user experience and content value.AnQiCMS with its flexible and customizable features provides many conveniences in the content operation field.

2025-11-06

How to perform keyword search on the AnQiCMS document list page?

Good, as an experienced website operations expert, I am happy to write a practical guide for you on how to perform keyword search in the AnQiCMS document list page. --- ## Efficient Keyword Search on AnQiCMS Document List Page: A Practical Guide Quickly locating and managing a large number of documents is crucial for improving efficiency in daily content operations.With the continuous enrichment of website content, we often need to accurately retrieve specific articles or products from the vast sea of information.

2025-11-06

How to implement the 'related articles' recommendation feature in AnQiCMS?

As an experienced website operations expert, I am well aware of the importance of the 'related articles' recommendation feature for improving user experience, extending user stay time, reducing bounce rate, and optimizing the internal link structure of the website (SEO).AnQiCMS (AnQiCMS) provides a flexible and efficient solution in this regard, allowing website operators to easily implement intelligent content recommendations.

2025-11-06

How to exclude certain specific categories of articles from the AnQiCMS document list?

During the operation of the website, we often need to manage and display content in a fine-grained manner.For example, we may wish to hide certain internal announcements, specific promotional content, or some draft category articles that are not suitable for external display when recommending articles on the homepage.AnQiCMS (AnQiCMS) is a powerful content management system that fully considers such needs and provides a simple and flexible way to achieve this - by specifying exclusion parameters in the document list tag.This article will delve into how to exclude certain categories of articles from the document list in AnQiCMS

2025-11-06

How to implement nested display of multi-level categories (such as product categories) in AnQi CMS?

AnQi CMS, as an enterprise-level content management system, provides high flexibility and powerful functions in content organization and display.For businesses, especially those with complex product lines or websites with a large amount of content, how to achieve clear and logical multi-level classification nesting display is the key to improving user experience and content management efficiency.Today, let's delve into how AnQiCMS elegantly achieves this goal.### The Foundation of AnQiCMS: Flexible Content Model and Classification System In AnQiCMS

2025-11-06

How to obtain the title, content, and link of a single page in AnQi CMS?

As an experienced website operations expert, I know that an efficient content management system plays a crucial role in the success of a website, especially in terms of flexible content display.AnQiCMS (AnQiCMS) provides us with great convenience with its powerful template functions and simple Go language architecture.Today, let's delve into how to easily retrieve the title, content, and link of a single page in AnQiCMS, so that you can better customize and display website information.### Getting to Know the "Single Page" in AnQi CMS In AnQi CMS, a single page (Single

2025-11-06

How to call the SEO title, keywords, and description of a single page in Anqi CMS?

As an experienced website operations expert, I am well aware of the importance of SEO for website visibility and business growth, and AnQiCMS indeed provides us with powerful and flexible tools in this regard.Today, let's delve into how to accurately call the SEO title, keywords, and description of a single page in AnQiCMS to ensure that each independent page can maximize its potential in search engines.

2025-11-06

How to display the name, description, and link of a single Tag in AnQiCMS?

In a content management system, tags (Tag) are an important tool for organizing content, enhancing user experience, and optimizing search engine rankings.AnQiCMS as an efficient enterprise-level content management system, naturally also provides powerful and flexible tag management functions.As an experienced website operations expert, I know that it is crucial to flexibly apply these functions to the front-end display for the usability and SEO of the website.Today, let's delve into how to accurately display the name, description, and link of a single tag in the AnQiCMS template.

2025-11-06