It is crucial to organize and present content when building and operating a website.尤其是当网站内容日益丰富,如何让访问者迅速找到他们感兴趣的信息,并清晰地浏览特定主题下的所有文章,就成了一个需要精心设计的问题。The AnQiCMS (AnQiCMS) provides powerful and flexible content management capabilities, allowing us to easily control and display the document list accurately.Today, let's discuss a common requirement: how to filter and loop through the document list under a specific category ID.This will help you better organize the website structure, improve user experience, and optimize the efficiency of content search.
Core Function Analysis:archiveListtags
In AnQiCMS, the core lies in usingarchiveListThis template tag. It is like a powerful query tool that can accurately capture the required documents from your content library.
archiveListThe label has multiple parameters that can help you accurately locate content, of which the one most closely related to category filtering iscategoryId.
Specify a single category IDWhen you want to display all documents under a specific category (such as "Company News" or "Product Introduction"), you can directly assign the ID of that category to
categoryIdParameters. For example, if the category ID of "Company News" is1You can use it like this:{% archiveList archives with categoryId="1" %}.Specify multiple category IDs: If you need to display documents under multiple non-adjacent categories,
categoryIdParameters also support. You only need to separate multiple category IDs with English commas,and separate them. For example,{% archiveList archives with categoryId="1,5,8" %}The output will be the document with category ID1/5and8.Control the number of displayed items and type:
archiveList标签还允许您通过limitParameter controls how many documents are displayed at one time, for example,limit="10"It means displaying 10 documents.typeThe parameter determines the type of the list,type="list"Used for general lists,type="page"which means you intend to use the pagination feature.Include or exclude subcategory documentsIn some cases, a large category may contain multiple subcategories.
childParameters can help you decide whether to include the documents of subcategories in the current list. By default,childYestrueEnglish translation: ,i.e., it will include documents of subcategories. If you only want to display documents directly associated with the current category, you can set it tochild=false.
English translation: Practical operation: Filter and loop through the document list
UnderstoodarchiveListThe label's key parameters, let's take a look at how to use it in the actual template to display your content.
Scenario one: output the latest documents under a specific category
Suppose you want to display the latest several documents under the category "Company News1)。Then,you can usearchiveListtags to retrieve these documents, andforoutput them one by one with loop tags.
{# 假设“公司新闻”的分类ID是 1 #}
<div class="news-list">
<h3>公司新闻</h3>
{% archiveList newsItems with categoryId="1" type="list" limit="5" order="id desc" %}
{% for item in newsItems %}
<article>
<h4><a href="{{item.Link}}">{{item.Title}}</a></h4>
<p>{{item.Description}}</p>
<time>{{stampToDate(item.CreatedTime, "2006-01-02")}}</time>
</article>
{% empty %}
<p>目前没有公司新闻发布。</p>
{% endfor %}
{% endarchiveList %}
</div>
In this code block:
- archiveList newsItems with category