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 tagmoduleId

  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.