In Anqi CMS, efficiently displaying the latest published article list is the foundation of website content operation, which can help keep your website vibrant and allow visitors to obtain the latest information first.AnQi CMS provides flexible and powerful template tags, allowing you to easily achieve this goal.
Understand the Core:archiveListThe charm of tags
To display the latest list of published articles, we mainly rely on the built-in AnqicmsarchiveListTemplate tag. This tag is the core tool for obtaining the list of various documents (such as articles, products, etc.) on the website. It is feature-rich and can be customized according to your specific needs.
Basic usage: Display the latest articles
The most common requirement is to simply display a few of the latest articles published on the website. To achieve this, you need to use the template file.archiveListLabel, and cooperate with several key parameters:
type="list": Tell the system that you want to get a non-paginated list.order="id desc": This is the key to specifying the sorting method.id descThis represents sorting documents by ID in descending order, as the ID of newly published articles is usually larger, which ensures that the latest articles are displayed at the top.limit="5": Controls the number of articles displayed, for example, displaying the most recent 5 articles.
Here is a simple example, which will list the latest 5 article titles and links on your page:
{% archiveList latestArticles with type="list" order="id desc" limit="5" %}
<ul>
{% for item in latestArticles %}
<li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
{% endfor %}
</ul>
{% endarchiveList %}
Here, latestArticlesThis is the variable name you give to this list of data, you can name it according to your preference. ByforLooping through this variable will allow you to display the details of each article one by one, for exampleitem.Linkget the article link,item.TitleGet the article title.
Customize deeply: meet more 'latest' needs
Of Security CMSarchiveListTags are not just simple lists of the latest articles, but they can also meet your more detailed 'latest' content display needs.
Display latest articles by category
If your website has multiple categories and you want to display only the latest articles of a specific category on a specific category page or module on the homepage, you can usecategoryIdParameter.
For example, to display the latest 10 articles under the "News动态" category:
{% archiveList newsArticles with type="list" categoryId="您的新闻分类ID" order="id desc" limit="10" %}
<h2>最新新闻</h2>
<ul>
{% for item in newsArticles %}
<li><a href="{{ item.Link }}">{{ item.Title }}</a> - {{ stampToDate(item.CreatedTime, "2006-01-02") }}</li>
{% endfor %}
</ul>
{% endarchiveList %}
here,categoryIdThe value should be replaced with your actual category ID. If you are calling from the current category page and have not specifiedcategoryIdThe system will automatically retrieve the articles under the current category.
Filter the latest list of content of a specific type.
The AnqiCMS supports a flexible content model, you may have distinguished between the 'article' model and the 'product' model. If you only want to display the latest released products instead of all documents, you can usemoduleIdParameter.
For example, get the latest 3 products:
{% archiveList latestProducts with type="list" moduleId="您的产品模型ID" order="id desc" limit="3" %}
<h3>最新产品推荐</h3>
<ul>
{% for item in latestProducts %}
<li><a href="{{ item.Link }}"><img src="{{ item.Thumb }}" alt="{{ item.Title }}"> {{ item.Title }}</a></li>
{% endfor %}
</ul>
{% endarchiveList %}
Please replacemoduleIdReplace it with the actual ID of your product model.
Highlight the latest articles marked as "Recommended" or "Headline"
The Anqi CMS allows you to set recommended attributes for articles (such as headline [h], recommended [c], etc.). If you want to highlight these contents in the latest article list, you can useflagParameter.
For example, display the latest 5 'Top Stories' articles:
{% archiveList featuredNews with type="list" flag="h" order="id desc" limit="5" %}
<div class="featured-articles">
<h4>头条速递</h4>
{% for item in featuredNews %}
<p><a href="{{ item.Link }}">{{ item.Title }}</a></p>
{% endfor %}
</div>
{% endarchiveList %}
Hereflag="h"Ensure that only articles marked as 'Top Stories' are displayed.
Optimize display: date and pagination
Standardize the display of publication time
When displaying the latest articles, the publication time is a very important piece of information. The article time field in Anqi CMS (such asCreatedTimeStored in timestamp format. To format it as a readable date, you can usestampToDate.
For example, convert the timestampitem.CreatedTimeto the format "2023-10-26":
{{ stampToDate(item.CreatedTime, "2006-01-02") }}
Here"2006-01-02"Is the time formatting standard in Go language, representing year-month-day. You can adjust the format as needed, such as"2006年01月02日 15:04"displayed as "October 26, 2023 10:30".
Add pagination to long lists
If the latest article list is long, displaying all articles at once will affect page loading speed and user experience. At this time, you should combine the use ofarchiveListandpaginationtags to achieve pagination display.
First, inarchiveListsettype="page"And specifylimitParameters to control the number of articles displayed per page:
{% archiveList allLatestArticles with type="page" order="id desc" limit="10" %}
<!-- 文章列表内容,与之前的for循环类似 -->
{% for item in allLatestArticles %}
<div class="article-item">
<h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
<p>发布时间:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</p>
</div>
{% endfor %}
{% endarchiveList %}
Following that, inarchiveListlabel's{% endarchiveList %}After that, usepaginationtags to generate pagination navigation:
<div class="pagination-nav">
{% pagination pages with show="5" %}
{% if pages.PrevPage %}<a href="{{ pages.PrevPage.Link }}">上一页</a>{% endif %}
{% for pageItem in pages.Pages %}
<a href="{{ pageItem.Link }}" class="{% if pageItem.IsCurrent %}active{% endif %}">{{ pageItem.Name }}</a>
{% endfor %}
{% if pages.NextPage %}<a href="{{ pages.NextPage.Link }}">下一页</a>{% endif %}
{% endpagination %}
</div>
show="5"This means that up to 5 page number buttons are displayed. In this way, your latest article list can be presented to users in a more friendly manner.
Improve efficiency and user experience
Aqie CMS is developed based on the Go language and has a natural advantage in handling high concurrency and data requests. It is配合 its built-in static caching mechanism, even frequent callsarchiveListTags can also ensure the response speed of the website. When designing templates, it is reasonable to plan the page structure, and make use ofincludeAn auxiliary label reuse code snippet not only improves development efficiency, but also makes template code more tidy and convenient for subsequent maintenance.
By flexible applicationarchiveList/stampToDateandpaginationTags, you can display the latest published article list in an efficient and beautiful way according to the actual needs of the website, thereby enhancing the perception of the frequency of content updates on the website and increasing user stickiness.
Frequently Asked Questions (FAQ)
Ask: I want to display the latest articles of different categories in different areas of the homepage, how should I operate?Answer: You can use the homepage template multiple times
archiveListtags, passing through each callcategoryIdParameters specify different category IDs. For example, one list displays the latest 5 articles of 'News Center', and another list displays the latest 3 articles of 'Industry Dynamics', simply pass in differentcategoryIdJust do it.Ask: Why did I set
order="id desc"But the publication date of the latest article list does not always seem to be the most recent?Answer:order="id desc"It is indeed arranged in descending order by article ID, which usually coincides with the publication time sequence.Please note that AnQi CMS provides the "timed release" function.If an article is set to a future publish time, even if it gets a larger ID when created in the background, it will not be displayed on the front page before reaching the publish time.Once the publish time is reached, it will appear and may be at the top of the list due to a larger ID.Please check the actual release time setting of the article.Ask: I want to display the latest articles while also showing the thumbnails and summaries of each article in the list. How can I achieve this?Answer: In
archiveListofforIn the loop, you can accessitemmultiple fields of an object. For example,item.ThumbCan retrieve the thumbnail image address of the article (provided that the article has set a thumbnail image),item.DescriptionYou can get the article summary. Just insert these fields into your list HTML structure. For example:<img src="{{ item.Thumb }}" alt="{{ item.Title }}"> <p>{{ item.Description }}</p>.