In website content management, effectively organizing and displaying articles is the key to improving user experience and website structure clarity.AutoCMS provides powerful and flexible template tag features, allowing you to easily retrieve and display all articles under a specific category.
Ensure the content structure is clear: the cornerstone of background category settings
Before starting operations at the template level, we must first ensure that the content structure of the backend is clear and reasonable.The classification management function of Anqi CMS is located under the "Content Management" module, where you can create and manage various article categories, such as "Company News", "Industry Dynamics", or "Technical Tutorials", etc.
When publishing or editing an article, you will find an option for 'Categories'. Articles are assigned to specific categories through this option.When you need to get a list of articles in a certain category, the system will filter them based on the category information you specify in the backend.
Core Function: UsearchiveListTag to get a list of articles
archiveListLabels are the core tools in AnQi CMS templates used to obtain article lists. They are powerful and can precisely control the range and display of articles to be obtained through various parameters.
To get articles under a specific category, the key is to usecategoryIdthis parameter. You can find the corresponding ID for each category in the "Content Management" -> "Document Category" in the background.
Additionally, you need to pay attention to the following important parameters:
type:This parameter determines the form in which you want to display the article list.- set
list(type="list"When it is “), it will display according to the quantity you specified (limitParameters list the articles, commonly used for sidebar recommendations or homepage article blocks. - set
page(type="page"When it is used, it will generate pagination for the article list, which is very useful in scenarios that require a large number of articles to be displayed, such as category article pages or search result pages.
- set
limit:Used to control the number of articles displayed per page or per time. For example,limit="10"10 articles will be displayed. It also supports offset mode, such aslimit="2,10"which means starting from the second article and getting 10 articles.order:The display order of the article is also important. You can set different sorting rules according to your needs:order="id desc": In reverse order by article ID, which usually means the most recently published articles are at the top.order="views desc":By article views in descending order, with popular articles first.order="sort desc":By backend custom sorting, allowing you to manually adjust the article order.
moduleId:If you manage multiple content models (such as having both 'Article Model' and 'Product Model' at the same time), make sure to specify the correct one.moduleIdThis can avoid confusion. The Article Model is usually set to 1 by default.
InarchiveListLabel inside, you will use aforloop to iterate over each article obtained. Each article within the loop is represented by aitemvariable, and you can useitem.字段名to obtain various information about the article, such as:item.Title(Title),item.Link(Link),item.Description[Summary],item.Thumb[Thumbnail],item.CreatedTime(Publish time) anditem.Views(views) and so on.
building template code: from simple list to pagination display
Understood these parameters, let's take a look at how to actually apply them in the template.
Example one: Get the latest 5 articles under a specific category (without pagination)
If you want to display several of the latest articles under a certain category on the homepage or sidebar, you can usetype="list". Suppose we want to get the latest 5 articles under the "Company News" category with ID1.
{# 获取ID为1的分类(例如“公司新闻”)下最新的5篇文章 #}
<div class="category-articles">
<h2>公司新闻</h2>
<ul>
{% archiveList articles with categoryId="1" type="list" limit="5" order="id desc" %}
{% for item in articles %}
<li>
<a href="{{ item.Link }}" title="{{ item.Title }}">
{# 如果有缩略图,则显示 #}
{% if item.Thumb %}
<img src="{{ item.Thumb }}" alt="{{ item.Title }}" class="article-thumb">
{% endif %}
<h3>{{ item.Title }}</h3>
<p class="article-desc">{{ item.Description|truncatechars:80 }}</p> {# 截取简介,避免过长 #}
<span class="article-date">{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
</a>
</li>
{% empty %}
{# 当该分类下没有文章时显示 #}
<li>该分类下暂无文章。</li>
{% endfor %}
{% endarchiveList %}
</ul>
</div>
In this example, we also used{{ stampToDate(item.CreatedTime, "2006-01-02") }}a filter to format timestamps into readable dates.truncatechars:80which is used to truncate the article summary so that it does not exceed 80 characters.
Example two: Get all articles under a specific category with pagination
When you need to display all articles under a category on the category details page, and provide pagination functionality,typeparameter settingspage, and cooperate withpaginationUse the label.
auto
{# 列表头部可以显示当前分类的名称和描述 #}
<div class="category-header">
<h1>{% categoryDetail with name="Title" %}</h1>
<p>{% categoryDetail with name="Description" %}</p>
</div>
<div class="articles-list">
{% for item in articles %}
<div class="article-card">
<h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
{% if item.Thumb %}
<a href="{{ item.Link }}"><img src="{{ item.Thumb }}" alt="{{ item.Title }}" class="article-card-thumb"></a>
{% endif %}
<p>{{ item.Description }}</p>
<div class="article-meta">
<span>发布时间:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
<span>浏览量:{{ item.Views }}</span>
</div>
</div>
{% empty %}
<p>该分类下暂无文章。</p>
{%