When managing website content in Anqi CMS, we often need to flexibly control the display of article lists, for example, only displaying articles under specific categories on a particular page.This not only helps to organize the content of the website accurately, but also greatly improves the browsing experience of visitors, helping them find the information they are interested in faster.AnQi CMS provides powerful template tag functions, making this requirement simple and intuitive.
The core of achieving this goal lies in Anqi CMS'sarchiveListTemplate tag. This tag is used to obtain the article list from the database, which is the '万能钥匙'. By configuring different parameters, we can accurately filter out the desired content.
UnderstandarchiveListthe critical parameters
InarchiveListThere are two crucial parameters in the tag that are used to filter articles of a specific category:
moduleIdThis parameter is used to specify the content model you want to retrieve for the article.In Anqi CMS, articles, products, and so on all belong to different content models.The ID of the 'Article Model' is 1, the ID of the 'Product Model' is 2, you can view or customize it in the 'Content Management' -> 'Content Model' on the backend.Ensure the specified is correctmoduleIdis the first step to obtaining the correct list of articles.categoryIdThis is the key parameter directly specifying the category of the article. By providing one or more category IDs, you can makearchiveListtags only return articles under these categories.
How to get the category ID?
In usecategoryIdBefore the parameter, you need to know the ID of the target category. There are several common methods to get the category ID:
- Obtained from the backend management interface:The most direct way is to log in to the Anqi CMS backend, go to 'Content Management' -> 'Document Categories'. Here, each category will clearly list its corresponding ID.
- Retrieve dynamically through template tags:If you are on a category detail page or its subpage and want to display articles of the current category, you can use
categoryDetailLabel to get the current category ID and then pass it toarchiveListFor example,{% categoryDetail with name="Id" %}You can directly output the current category ID.
Practical scenario: Accurate control of article list
MasteredmoduleIdandcategoryIdAfter parameters and how to obtain the category ID, we can meet various article list display requirements.
Firstly, assume that you want to display the latest articles under the specific category "Company News" in a block of the website.You need to find the 'Company News' category ID in the background first (for example, assuming its ID is 10), and the article model ID is usually 1.So, you can use it like this in the templatearchiveListTags:
{% archiveList newsArticles with moduleId="1" categoryId="10" limit="5" %}
{% for article in newsArticles %}
<div class="news-item">
<h4><a href="{{ article.Link }}">{{ article.Title }}</a></h4>
<p>{{ article.Description }}</p>
<span>发布日期:{{ stampToDate(article.CreatedTime, "2006-01-02") }}</span>
</div>
{% else %}
<p>暂无公司新闻。</p>
{% endfor %}
{% endarchiveList %}
This code will get the article model(moduleId="1") under the category ID 10(categoryId="10"The latest 5 articles in the bracket.
Secondly, if you need to display articles from multiple specific categories in a list, such as 'Company News' (ID: 10) and 'Industry Dynamics' (ID: 12), you just need tocategoryIdComma-separated to separate these category IDs:
{% archiveList mixedArticles with moduleId="1" categoryId="10,12" limit="10" order="CreatedTime desc" %}
{% for article in mixedArticles %}
<div class="article-card">
<h5><a href="{{ article.Link }}">{{ article.Title }}</a></h5>
<small>分类: {% categoryDetail with name="Title" id=article.CategoryId %}</small>
<span> | 日期: {{ stampToDate(article.CreatedTime, "2006-01-02") }}</span>
</div>
{% else %}
<p>暂无相关文章。</p>
{% endfor %}
{% endarchiveList %}
here,order="CreatedTime desc"Parameter used to sort articles by publish time in descending order to ensure the latest content is displayed.
Again, often we will build a page layout that displays content grouped by categories, such as displaying several category titles on the page, and then listing several articles under each title. This can be achieved by combiningcategoryListandarchiveListtags to implement:
{% categoryList mainCategories with moduleId="1" parentId="0" %} {# 获取文章模型下的所有顶级分类 #}
{% for category in mainCategories %}
<section class="category-block">
<h2><a href="{{ category.Link }}">{{ category.Title }}</a></h2>
<ul class="article-list">
{% archiveList articlesInCategory with moduleId="1" categoryId=category.Id limit="3" %} {# 显示当前分类下的3篇文章 #}
{% for article in articlesInCategory %}
<li><a href="{{ article.Link }}">{{ article.Title }}</a></li>
{% endfor %}
{% else %}
<li>该分类暂无文章。</li>
{% endarchiveList %}
</ul>
</section>
{% endfor %}
{% endcategoryList %}
In this example,categoryListTraversal of all top-level article categories, then for each category, nested thearchiveListUsecategory.IdParameters, dynamically retrieve and display the articles under the category.
Finally, if you want to display all articles under a specific content model but need to exclude one or more specific categories, such as not displaying articles from the category "Internal Announcement" (ID: 15), AnQi CMS also providesexcludeCategoryIdThe parameter meets this requirement:
{% archiveList allButInternal with moduleId="1" excludeCategoryId="15" limit="10" %}
{% for article in allButInternal %}
<div class="general-article-item">
<h3><a href="{{ article.Link }}">{{ article.Title }}</a></h3>
<span>分类:{% categoryDetail with name="Title" id=article.CategoryId %}</span>
</div>
{% else %}
<p>暂无文章可显示。</p>
{% endfor %}
{% endarchiveList %}
This line of code retrieves the content of all article models except the category with ID 15.
Tips and注意事项
In actual operation, please ensure that your template file conforms to the template naming and directory structure conventions of AnQi CMS, for example, category lists usually use{模型table}/list.htmlsuch naming format. At the same time,archiveListTags alsolimit(Limit the number of articles),order(Sorting methods, such as by publication time, views, etc.) as well astype(List type, such aslistUsed for simple lists,pageUsed for pagination lists) and more parameters, which can help you further refine the display logic of the article list.Use these parameters reasonably to make your website content more organized and professional.
By these flexible label combinations and parameter configurations, Anqi CMS allows you to easily handle the content of the website's articles, whether it is creating special pages, displaying recommended content, or building complex classification navigation, you can do it with ease.
Frequently Asked Questions (FAQ)
How to determine the article model?
moduleIdare?In most cases, the ID of the "Article Model" built into Anqi CMS is 1, and the ID of the "Product Model" is 2.You can log in to the backend management interface, go to "Content Management"->"Content Model", where you can clearly see each content model and its corresponding ID.If you customize a new content model, its ID will also be displayed here.Do I need to make additional settings to display the articles of the current category and all its subcategories in the article list?No additional settings are required.
archiveListThe tag supports fetching articles of the specified category and all its subcategories by default. That is to say, when you setcategoryIdWhen a category ID is passed as a parameter, it will automatically include all articles under the category. It is only necessary to use it when you explicitly do not want to include articles from subcategories.child="false"Parameter.Why did I set
categoryIdAfter, the article list is still empty or displays incorrectly?This may be caused by several reasons. First, please checkmoduleIdandcategoryIdAre they correct, make sure they match the actual content model and category IDs on the backend.Next, confirm that there is indeed an article published (not a draft or in the recycle bin) under this category.Also, checklimitwhether the parameters are set correctly?