Easily get the list of articles under a specified category in AnQiCMS
As an experienced website operations expert, I fully understand the core value of a content management system (CMS) lies in its ability to organize and present content.Among many functions, how to efficiently extract and display articles from a specific category is an indispensable part of the daily work of website operators.Today, let's delve into how AnQiCMS can help you achieve this goal, making technical details intuitive and easy to understand.
AnQiCMS as an enterprise-level content management system based on the Go language, with its concise and efficient architecture, provides us with a flexible and powerful content display mechanism.Among them, obtaining the list of articles under a specified category is achieved through its carefully designed template tags.
Understand the Core:archiveListTemplate Tag
In AnQiCMS, to get the list of articles under a specified category, we mainly rely on a powerful template tag:archiveList. This tag is like an intelligent filter that can accurately find the required articles from a vast content library based on the conditions provided and present them.
When you need to display articles of a specific category in the AnQiCMS template file, the first thing that comes to mind isarchiveList. Its most common usage is withcategoryIdCombine parameters to clearly tell the system which category of articles you want to retrieve. For example, if you have a category ID of 10 for the "Company News" section, you can usecategoryId="10"Specify this category.
Let's look at a basic structure:
{% archiveList archives with categoryId="10" limit="5" order="id desc" %}
{% for item in archives %}
<!-- 在这里展示每篇文章的详细信息 -->
<h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
<p>{{ item.Description }}</p>
<p>发布时间:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</p>
<hr>
{% empty %}
<p>该分类下暂无文章。</p>
{% endfor %}
{% endarchiveList %}
In this code block,archivesIs a variable name we define to storearchiveListThe collection of article data returned by the tag.limit="5"Tell the system to only display the latest 5 articles, andorder="id desc"Then ensure that these articles are sorted in descending order by ID, that is, the most recently published articles come first.{% for item in archives %}The loop then iterates over these articles,itemThe variable represents the current article being traversed, you canitem.Title/item.Link/item.Descriptionaccess the various attributes of the article in this way. If there are no articles under the category,{% empty %}the content in the block will be displayed.
Wisdom in dynamically acquiring category ID
In actual operation, the classification ID is often not fixed.For example, when you visit a category page, you may want to automatically display the articles under that category instead of manually entering the category ID.AnQiCMS also provides an elegant solution.
You can usecategoryDetailTag to dynamically acquire the category information of the current page, including its ID.
{# 假设当前页面是一个分类列表页 #}
{% categoryDetail currentCategory with name="Id" %}
{% archiveList archives with categoryId=currentCategory limit="10" order="views desc" %}
{% for item in archives %}
<h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
<p>浏览量:{{ item.Views }}</p>
{% endfor %}
{% endarchiveList %}
Here, currentCategoryThe variable stores the category ID of the current page and passes it toarchiveListofcategoryIdParameters. This way, no matter which category page the user visits, the articles under that category will be displayed automatically.
Deep Dive: Flexible Application of More Parameters.
archiveListThe power of the label goes far beyond this, it also provides various parameters to meet more complex needs:
moduleId: AnQiCMS supports flexible content models (such as articles, products, etc.). If you only want to retrieve articles under a specific content model, you can setmoduleIdFor example,moduleId="1"It usually represents the article model.child: By default,archiveListit will retrieve articles under the specified category and all its subcategories. If you only want to display articles under the current category and not include the content of subcategories, you can setchild=false.typeThis parameter is very critical,type="list"indicating to retrieve a fixed number of article lists, whiletype="page"it is used to generate paginated article lists, usually withpaginationUse tags together to enable the "previous page/next page" navigation function.flag: If your article has set recommendation attributes (such as头条[h], 推荐[c] etc.), you canflag="c"filter articles with specific recommendation attributes.q: Use the search page toqspecify search keywords,archiveListwhich will return articles with the specified keyword in the title.
Example: CombinecategoryListto display articles from multiple categories in sequence.
A common requirement is to display multiple categories and the latest articles under them on the homepage or some aggregation page. This can be achieved by nestingcategoryListandarchiveListtags to implement:
{% categoryList categories with moduleId="1" parentId="0" %} {# 获取所有顶级分类 #}
{% for category in categories %}
<section>
<h2><a href="{{ category.Link }}">{{ category.Title }}</a></h2>
<ul>
{% archiveList articlesInCategory with categoryId=category.Id limit="5" order="id desc" %}
{% for article in articlesInCategory %}
<li>
<a href="{{ article.Link }}">{{ article.Title }}</a>
<span>({{ stampToDate(article.CreatedTime, "01-02") }})</span>
</li>
{% empty %}
<li>此分类下暂无内容。</li>
{% endfor %}
{% endarchiveList %}
</ul>
</section>
{% endfor %}
{% endcategoryList %}
This code first retrieves all top-level article categories, and then for each category, retrieves the 5 latest articles under it. BystampToDateFilter, we can also format the publication time of the article into a concise 'month-day' format.
Optimization of template file selection
AnQiCMS is also very flexible in template design, supporting the use of different templates for different categories. For example, you can create{模型table}/list-{分类ID}.htmlThis template file allows a specific category to have a unique list display style. This is very helpful for achieving personalized content layout and SEO optimization.
By using these tags and techniques, you can effortlessly obtain and display the article list under the specified category in AnQiCMS, whether it is a simple list display or a complex aggregation page, you can easily deal with it.Effectively utilize these tools will greatly enhance the management efficiency and user experience of your website content.
Frequently Asked Questions (FAQ)
Q1: How do I get all articles under all categories?
If you want to get all articles under all categories on the website instead of specifying a single category, you can usearchiveListOmitting the labelcategoryIdParameters, AnQiCMS will default to returning all articles. Of course, for performance considerations, you would usually cooperate withlimitParameters to limit the number of articles returned, or usetype="page"andpaginationTags are displayed by pagination.
Q2: If my articles have different content models (such as 'news' and 'product'), how can I retrieve them separately?
AnQiCMS supports custom content models, each model has a uniquemoduleId. When you usearchiveListtags, you can go throughmoduleId="X"Specify the article of the content model you want to retrieve,Xis the ID corresponding to the content model. For example, if the article modelmoduleIdis 1, the product modelmoduleIdis 2, you can set upmoduleId="1"Get only the articles.
Q3: I want to display the sub-categories of the category on the category list page at the same time, and list the articles under the sub-categories. Can this be done?
It can be done. You can use nestingcategoryListTags to achieve multi-level classification display. In the outer layercategoryListIn the loop, determine if the current category has subcategories (item.HasChildren), if so, use another onecategoryListTag retrieval and traversal of its subcategories; if there are no subcategories, or if you want to display articles below the subcategories, you can embed it in the corresponding looparchiveListthe tag and use it.categoryIdThe parameter is set to the ID of the current category.In the document, the example of