In AnQi CMS, implementing a feature to aggregate and display a list of multiple article categories on a single page is a very practical requirement. It can help website administrators organize content efficiently and provide visitors with a more convenient browsing experience.This is typically applied to the homepage, special pages, or aggregation pages of a website. By skillfully using AnQiCMS template tags, we can easily achieve this goal.
Core idea: Tag combination and nested loops
AnQi CMS provides a flexible template tag system, wherecategoryListandarchiveListare the two core players in content aggregation display. Our basic idea is:
- Firstly, get the category list you want to aggregate and display.This could be a top-level category or a child category under a parent category.
- Then, traverse these categories.In each iteration, we will get the detailed information of the current category.
- Within each category, we will get the list of articles under the current category ID and display them.So, each category's articles will be presented in the form of independent blocks on the page.
We can clearly display multiple categories and their contained article content on a single page through this combination of tags and nested loops.
Preparation: Understand the template structure and content model
Before you start, it is recommended that you first confirm the structure and content model of your AnQiCMS template. The template files of AnQiCMS are usually stored in/templatedirectory, with.htmlSuffix. You can choose to place these codes in the homepage template of the website (index.html), on a single page (page/detail.html), or create a custom template for a specific aggregation page to place these codes.
In addition, AnQiCMS supports flexible content models such as 'article model', 'product model', etc.When fetching the category and article list, make sure you specify which content model's data you want to retrieve, as different models have their own independent categories and article data.moduleIdspecified by parameters, for example,moduleId="1"usually represents an article model,moduleId="2"represents a product model.
application of key template tags
1.categoryList: Get the category list
categoryListTags are used to get the list of categories of a website. We can use it to filter out the categories that need to be displayed on the page.
moduleId: Specify the content model ID, for examplemoduleId="1"(article model).parentId指定parent category ID.parentId="0"Represents getting all top-level categories. If you want to get the subcategories under a specific category, you can fill in the ID of that category.- Loop variable
itemCommon fields of:Id(Category ID),Title(Category Name),Link(Category Link),Description(Category Description) et al.
2.archiveList: Get article list
archiveListTags are the core tags for obtaining articles (or products, depending on)moduleId).
type="list": It means to display in list form, usually paired withlimitParameter.categoryId: This is the most critical parameter, it will be associated with externalcategoryListBind the category ID in the loop, ensuring that the articles under the current category are obtained.moduleId: Similarly, you need to specify the content model ID, withcategoryListofmoduleIdconsistency.limitControls the number of articles displayed under each category, for example,limit="6"It means 6 articles are displayed under each category.order: Specifies the sorting method of the articles, for example,order="id desc"(Sorted by ID in descending order, i.e., latest published),order="views desc"(Sort by view count in descending order).- Loop variable
archiveCommon fields of:Id(Article ID),Title(Article Title),Link(Article Link),Description(Article Summary),Thumb[Thumbnail],CreatedTime[Publish time],Views[Views] etc.
3.forLoops and Conditional Judgment
forLoop is iterationcategoryListandarchiveListThe basic way to return list data.ifConditional judgment is used to control the display of content in specific situations, such as checking if there is a thumbnail, or handling the case where the list is empty.
4.stampToDateTime formatting
When displaying the article publish time,archive.CreatedTimeThe returned value is a timestamp, we can usestampToDatetags to format it into an easily readable date format, such as{{stampToDate(archive.CreatedTime, "2006-01-02")}}.
5.categoryDetail: Get category details
Although it iscategoryListofforpossible to access directly in a loopitem.Titleanditem.Link, but in some cases that require obtaining more detailed classification information (such as classification imagesLogoorThumb),categoryDetailTag combinationid=item.Idthe parameters will be more convenient.
Practical Code Examples: Aggregate Display of Articles from Multiple Categories
Assuming we want to aggregate and display several top-level categories under the "Article Model" on the homepage, with each category showing the latest 6 articles. You can do this in your homepage template file (for example,)/template/default/index.htmlAdd the following code in the bracket:
【en】External loop: Get the top-level category list of the article model {% categoryList categories with moduleId=“1” parentId=“0” %}
{% for item in categories %}
<div class="category-block">
{# 显示分类标题和链接 #}
<h3 class="category-title">
<a href="{{ item.Link }}" title="更多{{item.Title}}">{{item.Title}}</a>