It is crucial to organize and present content when building and operating a website.Especially when website content becomes increasingly rich, how to allow visitors to quickly find the information they are interested in and clearly browse all articles under a specific topic has become a problem that requires careful design.AnQiCMS provides powerful and flexible content management capabilities, allowing us to easily achieve precise control and display of document lists.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 search efficiency of content.
Core function analysis:archiveListTag
In AnQiCMS, the core is to implement filtering and outputting document lists according to category ID, usingarchiveListThis template tag. It is like a powerful search tool that can accurately fetch the required documents from your content library.
archiveListTags have multiple parameters that can help you accurately locate content, 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 category's ID to
categoryIdParameter. For example, if the category ID of "Company News" is1, you can use it like this:{% archiveList archives with categoryId="1" %}.Specify multiple category IDs: If you need to display documents from multiple discontinuous categories at the same time,
categoryIdParameters are also supported. You only need to separate multiple category IDs with English commas,and it will be separated. For example,{% archiveList archives with categoryId="1,5,8" %}The output will be the category ID of1/5and8documents.Control the number and type of display:
archiveListtags also allow you to refine the definition of “relevant” throughlimitParameters control how many documents are displayed at once, for examplelimit="10"It means displaying 10 documents.typeThe parameter determines the type of the list,type="list"Used for regular lists,type="page"It means you plan to use the pagination feature.Include or exclude subcategory documentsIn some cases, a large category may contain multiple subcategories.
childThe parameter can help you decide whether to include the documents of subcategories in the current list. By default,childIstrueIt will include documents with subcategories. If you only want to display documents directly associated with the current category, you can set it tochild=false.
Practical operation: Filter and loop through the document list
UnderstoodarchiveListAfter the key parameters of the tag, let's see how to apply 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 documents under the "Company News" category on the homepage or a specific thematic page. First, you need to know the category ID of "Company News" (assuming it is1)。Then,you can usearchiveListtags to retrieve these documents and combineforloop tags to output them one by one.
{# 假设“公司新闻”的分类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