When building a website with AnQi CMS, we often encounter the following requirements: On a category page, we want to display only the documents that belong to the current category and not mix in the content of the documents from its subcategories.This is particularly important in scenarios where the content structure is clear and redundant information is avoided.Today, let's delve into how to accurately achieve this goal through the template tags of Anqi CMS.
Understanding the relationship between AnQiCMS categories and documents
Core strategy:archiveListlabel'schildParameter
To achieve the goal of displaying only the documents under the current category and not including the documents of subcategories, we need to focus on the core isarchiveListThe tag is specifically used to retrieve document lists, it provides a parameter namedchildwhich is the key to controlling this behavior.
childThe parameter supportstrueandfalsetwo values:
child=true(default value)This indicates that when querying documents, it will not only include the documents under the currently specified category but will also recursively include all subcategories (including the subcategories of subcategories, and so on) within these subcategories.child=falseThis indicates that when querying documents, it will strictly limit to the current specified category and will not retrieve any documents from its child categories.
Therefore, our solution is to usearchiveListWhen labeling, set it clearlychild=false.
Let's understand through a specific example. Suppose you are designing a category page template, and you want to list only the articles under the current category on this page.
{# 假设这是分类页面模板,我们首先需要获取当前分类的ID和标题 #}
{% categoryDetail currentCategory with name="Id" %} {# 获取当前分类的ID,并赋值给 currentCategory 变量 #}
{% categoryDetail categoryTitle with name="Title" %} {# 获取当前分类的标题,并赋值给 categoryTitle 变量 #}
<div class="category-header">
<h1>{{categoryTitle}}</h1>
<p>以下是“{{categoryTitle}}”分类下的所有文档(不含子分类)。</p>
</div>
<div class="document-list">
{# 使用 archiveList 标签,并设置 categoryId 为当前分类ID,child=false 确保不包含子分类文档 #}
{% archiveList archives with type="page" categoryId=currentCategory child=false limit="10" %}
{% for item in archives %}
<div class="document-item">
<h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
<p>{{item.Description}}</p>
<small>发布于: {{stampToDate(item.CreatedTime, "2006-01-02")}} | 浏览量: {{item.Views}}</small>
</div>
{% empty %}
<p>抱歉,当前分类({{categoryTitle}})下没有找到文档。</p>
{% endfor %}
{# 如果需要分页,可以在此处添加分页标签 #}
{% pagination pages with show="5" %}
<div class="pagination-controls">
{% if pages.PrevPage %}<a href="{{pages.PrevPage.Link}}">上一页</a>{% endif %}
{% for p in pages.Pages %}<a href="{{p.Link}}" class="{% if p.IsCurrent %}active{% endif %}">{{p.Name}}</a>{% endfor %}
{% if pages.NextPage %}<a href="{{pages.NextPage.Link}}">下一页</a>{% endif %}
</div>
{% endpagination %}
{% endarchiveList %}
</div>
In this example:
- We first use
categoryDetailThe tag fetched the category ID of the current page (currentCategory) and the title (categoryTitle) - Next, in
archiveListIn the tag, we willcategoryIdthe parameter tocurrentCategory, to ensure the document query is limited to the current category. - The most critical step is to set
child=falseThis parameter tells AnQi