As an experienced CMS website operation personnel in the field of information security, I am well aware of the importance of content organization and display for user experience and SEO.In daily work, we often need to filter and present article lists according to specific categories so that readers can quickly find the content they are interested in.Today, I will elaborate in detail on how to flexibly and efficiently filter and display the list of articles in AnQiCMS according to the specified category ID.
The categorization of content is the foundation for the effective operation of a website.Whether it is news information, product introduction, or blog articles, through precise classification, it can not only enhance the clarity of the website structure but also help search engines better understand the content, thereby bringing better traffic.In AnQiCMS, we utilize the powerful template tag features to easily achieve this goal.
Core Features and Implementation Principles
The core tag used to retrieve article lists in the AnQiCMS template system isarchiveList.This tag provides rich and flexible parameters, allowing us to filter articles based on various conditions, including filtering by category ID.It can handle articles of different content models (such as articles, products) and supports various modes such as ordinary list display, paginated list display, and related article display.archiveListTags, we can accurately extract article data that meets specific classification conditions from the database and render it in the front-end template.
Determine the target classification ID and content model
Before starting to write code in the template, you first need to clarify which category of articles you want to display, and obtain the unique identifier of that category - the category ID.At the same time, AnQiCMS supports various content models (such as, the default article model ID is 1, and the product model ID is 2). You also need to determine the content model ID for these articles.This information is usually found in the content management area of the AnQiCMS backend, each category and content model has its corresponding ID.5and the article belongs to the default 'Article Model' (moduleIdusually is1).
called in the templatearchiveListtags
Once the target category ID and content model ID are obtained, they can be used in the template files of AnQiCMSarchiveListCall the article list with a tag. This tag usually needs to be wrapped in a{% archiveList ... %}and{% endarchiveList %}structure, and use{% for ... %}to iterate over the retrieved article data.
The following is a basic code example demonstrating how to get the articles under the category ID of5the first 10 articles:
{% archiveList archives with type="list" moduleId="1" categoryId="5" limit="10" %}
{% for item in archives %}
<li>
<a href="{{ item.Link }}">
<h5>{{ item.Title }}</h5>
<p>{{ item.Description }}</p>
<span>发布日期: {{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
<span>阅读量: {{ item.Views }}</span>
</a>
</li>
{% empty %}
<li>该分类下暂时没有文章。</li>
{% endfor %}
{% endarchiveList %}
In the above code:
archivesThis is the variable name we define for the article list, you can use any meaningful name.type="list"This indicates that we want to display the articles in list form, rather than in pagination mode.moduleId="1"Specified the content model as article model.categoryId="5"It is a key parameter for filtering articles based on category ID.limit="10"It limits the number of displayed articles to 10.{% for item in archives %}The loop will process each article one by one.{% empty %}The block will bearchivesDisplay alternative content when the list is empty.
Custom list display and data fields
archiveListTag returnsitemThe object includes various attributes of the article, and you can flexibly call them in the template according to your actual needs. Common fields include:
item.Title: Article titleitem.LinkArticle linkitem.DescriptionArticle summaryitem.Thumboritem.Logo: The thumbnail or cover image of the articleitem.CreatedTimeArticle publish timestamp (required to be provided)stampToDateTag formatting)item.ViewsArticle viewsitem.CategoryId:Article category ID
To make the article list more diverse, we can also combine other tags to get more information. For example, to display the name of the category to which the article belongs, you can usecategoryDetailTags:
{% archiveList archives with type="list" moduleId="1" categoryId="5" limit="10" %}
{% for item in archives %}
<div class="article-item">
<a href="{{ item.Link }}">
<h4>{{ item.Title }}</h4>
{% if item.Thumb %}
<img src="{{ item.Thumb }}" alt="{{ item.Title }}" class="article-thumb">
{% endif %}
<p>{{ item.Description }}</p>
<div class="article-meta">
<span>分类: {% categoryDetail with name="Title" id=item.CategoryId %}</span>
<span>发布于: {{ stampToDate(item.CreatedTime, "2006年01月02日") }}</span>
<span>阅读量: {{ item.Views }}</span>
</div>
</a>
</div>
{% endfor %}
{% endarchiveList %}
Here,{% categoryDetail with name="Title" id=item.CategoryId %}be able to base on the current article'sCategoryIdDynamically retrieve and display its category name, greatly enhancing the relevance of content and user experience.
Implement pagination for the article list (optional)'} ]
For categories with a large number of articles, we usually need to implement pagination to optimize loading speed and browsing experience.archiveListTags are set throughtype="page"parameters, and combinedpaginationTags, can easily realize the pagination list.
{% archiveList archives with type="page" moduleId="1" categoryId="5" limit="10" %}
{# 文章列表内容与上述类似 #}
{% for item in archives %}
<div class="article-item">
<a href="{{ item.Link }}">
<h4>{{ item.Title }}</h4>
<p>{{ item.Description }}</p>
<div class="article-meta">
<span>分类: {% categoryDetail with name="Title" id=item.CategoryId %}</span>
<span>发布于: {{ stampToDate(item.CreatedTime, "2006年01月02日") }}</span>
</div>
</a>
</div>
{% endfor %}
{% empty %}
<p>该分类下暂时没有文章。</p>
{% endarchiveList %}
{# 分页导航 #}
<div class="pagination-container">
{% pagination pages with show="5" %}
<ul>
<li {% if pages.FirstPage.IsCurrent %}class="active"{% endif %}><a href="{{ pages.FirstPage.Link }}">{{ pages.FirstPage.Name }}</a></li>
{% if pages.PrevPage %}
<li><a href="{{ pages.PrevPage.Link }}">{{ pages.PrevPage.Name }}</a></li>
{% endif %}
{% for pageItem in pages.Pages %}
<li {% if pageItem.IsCurrent %}class="active"{% endif %}><a href="{{ pageItem.Link }}">{{ pageItem.Name }}</a></li>
{% endfor %}
{% if pages.NextPage %}
<li><a href="{{ pages.NextPage.Link }}">{{ pages.NextPage.Name }}</a></li>
{% endif %}
<li {% if pages.LastPage.IsCurrent %}class="active"{% endif %}><a href="{{ pages.LastPage.Link }}">{{ pages.LastPage.Name }}</a></li>
</ul>
{% endpagination %}
</div>
Here,type="page"indicatearchiveListGenerate pagination data,paginationTags are responsible for rendering pagination navigation links,show="5"The maximum number of page buttons displayed is 5. This ensures that even with a large number of articles under a category, the performance and readability of the page are maintained.
**Practical Suggestions and Optimization**
In actual operation, properly utilizing these tags can greatly enhance the efficiency of content management and the user experience of the website. In addition to the above examples, you can also usearchiveListother parameters for more refined filtering, such as sorting by publication time (order="id desc"), sorting by view count (order="views desc"), filtering by recommended attributes (flag="c"recommended articles), etc. Make sure to pay attention to the naming conventions and directory structure of the template files, as AnQiCMS supports accessing{模型table}/list-{分类ID}.htmlIn this way, custom templates for specific categories are created, which provides great convenience for personalized display.
Provided by AnQiCMSarchiveListTags and their rich parameters provide powerful support for website operators to build highly customized content display pages.By understanding and mastering these features, you can easily create a clear, rich, and user-friendly website experience according to the reader's needs.
Common Questions and Answers (FAQ)
1. How to display a list of articles from multiple categories at the same time?
If you need to display articles from multiple specified categories on a single page, you canarchiveListTagscategoryIdseparate multiple category IDs with commas in the parameters. For example,categoryId="1,5,8"The articles with category IDs 1, 5, and 8 will be displayed simultaneously.
2. How to ensure that only articles of a specific content model (such as articles or products) are displayed in the list?
InarchiveListIn the tags, you can go throughmoduleIdThe parameter is used to specify the ID of the content model. For example,moduleId="1"It is usually used to display the content of the article model,moduleId="2"While it may be used to display the content of the product model. Make sure you usemoduleIdMatch the content type you want to display.
3. How to implement pagination for article lists?
To implement pagination for article lists, you need to inarchiveListsetting in the labeltype="page"the parameters. Then, inarchiveListAfter the tag closure, usepaginationtag to render pagination navigation.paginationThe tag acceptsshowparameters to control the number of displayed page numbers, for example{% pagination pages with show="5" %}.