As an experienced CMS website operation personnel, I fully understand the core position of content in website operation.Dynamically displaying the latest content can not only improve user experience but also effectively increase the activity of the website and the frequency of search engine crawling.Below, I will elaborate on how to loop and display the list of the latest 10 published articles in the AnQiCMS template.
In AnQiCMS template, the latest 10 published articles list is displayed in a loop
In AnQiCMS, the creation and management of templates follow a set of simple and efficient rules, whose syntax is similar to the Django template engine, making it easy for front-end developers to get started.To dynamically display the latest 10 articles on the website page, we need to use the built-in template tags and loop structures of AnQiCMS.
安企CMS的模板文件通常以 English.html为后缀,并存放在 English/templateThe specific template folder under the catalog. Static resources such as styles, scripts, and images are placed in/public/static/Directory. Variables are used with double curly braces in template files.{{变量}}Definitions are specified using single curly braces and percent signs, while logical tags for condition judgments and loop controls are used.{% 标签 %}Definition, and it needs to be closed with an end tag, for example{% for ... %}Required{% endfor %}.
To get the list of articles, we mainly rely on the AnQiCMS providedarchiveListLabel.This tag is a powerful tool for getting lists of articles, products, and other content.By properly configuring its parameters, we can accurately control the quantity, sorting method, and category of the required content.
Firstly, we need to use the template file where we want to display the latest articles.archiveListLabel to retrieve data.This tag allows us to specify multiple conditions to filter articles, the most critical of which is to limit the number and sort the way.typeparameter settings"list"(This indicates getting a list instead of pagination),limitparameter settings"10"(Limited to fetching 10 articles), and useorderThe parameter specifies the sorting method as"CreatedTime desc". This means that the articles will be sorted in descending order by creation time, ensuring that the most recently published articles are displayed at the top. In addition,moduleIdThe parameter is used to specify the content model, articles usually use the default article model ID, and generally do not need to be specified specifically or filled in according to actual configuration.
{# 获取最新发布的10篇文章 #}
{% archiveList archives with type="list" limit="10" order="CreatedTime desc" %}
{# 检查是否有文章存在,如果列表为空,则显示“没有内容” #}
{% for item in archives %}
<div class="latest-article-item">
{# 文章标题及链接 #}
<h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
{# 文章发布时间,使用stampToDate标签格式化 #}
<p class="article-date">发布于:{{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}</p>
{# 文章简介 #}
<p class="article-description">{{ item.Description }}</p>
{# 如果文章有缩略图,则显示 #}
{% if item.Thumb %}
<div class="article-thumbnail">
<img src="{{ item.Thumb }}" alt="{{ item.Title }}">
</div>
{% endif %}
{# 可以根据需要添加更多文章信息,例如分类、浏览量等 #}
<p class="article-meta">
{% if item.CategoryId %}
{# 获取文章所属分类的名称和链接 #}
<span>分类:<a href="{% categoryDetail with name='Link' id=item.CategoryId %}">{% categoryDetail with name='Title' id=item.CategoryId %}</a></span>
{% endif %}
<span>浏览量:{{ item.Views }}</span>
</p>
</div>
{% empty %}
<p>目前还没有任何最新文章发布。</p>
{% endfor %}
{% endarchiveList %}
In the above code,{% archiveList archives with type="list" limit="10" order="CreatedTime desc" %}This line of code is the core, it retrieves the article data that meets the conditions from the database and stores the result in a variable namedarchives.{% for item in archives %}Loop through thisarchivesArray, each loop will assign the data of the current article toitema variable.
We can access within the loop.itemThe various properties of the object to display the detailed information of the article, such as{{ item.Title }}To get the article title,{{ item.Link }}Get the article detail page link,{{ item.Description }}Get the article summary. Particularly,{{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}This line of code shows how to usestampToDatethe tag to format Unix timestamp into a readable date and time. The"2006-01-02 15:04"is a date formatting string specific to Go language. We also use{% if item.Thumb %}to determine if the article has a thumbnail and display it. By nestingcategoryDetailLabels, which can obtain the name and link of the category to which each article belongs.
IfarchiveListThe list of articles obtained is empty.{% empty %}The content within the label will be executed, providing friendly prompt information. Finally,{% endarchiveList %}and{% endfor %}Labels are used for closingarchiveListandforLoop.
In practical application, please be sure to place the above code snippet in your AnQiCMS template file (for exampleindex.htmlor a public segment file you customize).Also, make sure that your template file uses UTF-8 encoding to avoid garbled character issues.Any modification to the template file may require you to click "Update Cache" in the background management interface to ensure that the content of the front-end page is refreshed in a timely manner.By this means, your website can dynamically display the latest and most attractive content, thereby better serving your readers.
Common Questions (FAQ)
1. How to modify the number of displayed articles or adjust the sorting method?
You can adjust byarchiveListthe tag inlimitandorderparameters to easily achieve this. For example, if you want to display 5 articles,limit="10"tolimit="5". If you want to sort the articles by views in descending order (the most popular articles), you canorder="CreatedTime desc"toorder="views desc". These parameters provide great flexibility to meet your different content display needs.
2. How do I display the latest articles under a specific category?
InarchiveListtag.categoryIdparameter. For example, if you only want to display the latest articles under category ID 10, you can setarchiveListthe tag to{% archiveList archives with type="list" limit="10" order="CreatedTime desc" categoryId="10" %}. You can also separate multiple category IDs with commas, such ascategoryId="10,12,15", to display the latest articles under multiple specified categories.
3. Can this code be used to display content models other than articles, such as the latest products?
It can be. The design of AnQiCMS allows the reuse of the same list tags for different content models. If you want to display the latest products, you need to know the corresponding product model.moduleId。usually, the article modelmoduleIdis 1, the product modelmoduleIdis 2 (specific values please refer to your AnQiCMS backend content model configuration). Just inarchiveListput in the tagmoduleIdThe parameter is set to the product model ID, for example{% archiveList products with type="list" limit="10" order="CreatedTime desc" moduleId="2" %}, thenarchiveschange the variable name toproducts(or any name you like) to keep the code clear, and it can display the latest product list.