In website content management, we often need to dynamically display the latest updates of a certain type of content, such as news articles, product releases, or blog updates.For websites built using AnQiCMS, leveraging its powerful API interface to obtain the latest document list under a specified model is a very basic and practical operation.This not only helps developers quickly build dynamic pages, but also provides users with timely updated information to optimize the website experience.
To implement this feature, we mainly use the AnQiCMS provided/api/archive/listThe document list interface. This interface is very flexible, and by combining different parameters, we can accurately filter, sort, and obtain the desired content.
Understandingarchive/listInterface
First, let's understand this core interface. Its call address is usually{域名地址}/api/archive/listAnd thenGETrequest is sent by the method. Here is{域名地址}Replace with your actual website domain, for examplehttps://en.anqicms.com/api/archive/list.
This interface has many parameters, but in order to getList of latest documents under the specified modelWe need to pay special attention to the following:
moduleId(Model ID)This is a required parameter in the request. Each content model created in the AnQiCMS backend (such as "articles", "products", etc.) has a unique numeric ID.Specify throughmoduleIdWe tell the system which model's documents we want to retrieve.order(Sorting method)This is the key to achieving the "latest" effect. AnQiCMS supports sorting by multiple fields, including those related to time such ascreated_time(Publishing time) andupdated_time(Update time). To get the latest content, we will set it tocreated_time descorupdated_time descof whichdescmeans in descending order, that is, the latest in front.limit(Display number)This parameter is used to control how many of the latest documents you want to retrieve. For example, if you want to display the most recent 5 articles, you can setlimit=5If you need to retrieve a certain number of data from a certain record, you can also useoffset,limitformat, such aslimit=2,10It means to retrieve 10 data from the 3rd record.type(List type): By default,typeThe value islistIt will directly return the specified number of documents. If you need to display pagination (such as showing the total number of pages or total number of items), then you can usetypeis set topageat this time, you can usepageParameters can retrieve the content of a specified page. However, for simple "latest list" needs, keep the default.type=listJust do it.
Actual operation example
Assuming your "article" model'smoduleIdIs1,You now want to get the 5 latest articles released under this model. You can construct the API request in the following way:
GET {您的域名}/api/archive/list?moduleId=1&order=created_time desc&limit=5
After you send this GET request, AnQiCMS will return a JSON formatted data. In the case of success,codethe field will be0whiledataThe field will contain the document list you requested.
The data structure returned is roughly as follows:
{
"code": 0,
"data": [
{
"id": 101,
"created_time": 1705000000, // 较新的发布时间
"updated_time": 1705000050, // 较新的更新时间
"title": "AnQiCMS新功能发布:AI智能写作模块上线!",
"description": "详细介绍AnQiCMS最新推出的AI智能写作功能,帮助您高效创作内容。",
"module_id": 1,
"category_id": 5,
"views": 120,
"thumb": "https://yourdomain.com/uploads/images/ai-feature.webp",
"link": "https://yourdomain.com/news/101.html",
// ... 其他文档信息
},
{
"id": 100,
"created_time": 1704900000,
"updated_time": 1704900030,
"title": "AnQiCMS 3.0版本更新日志",
"description": "回顾AnQiCMS 3.0版本的各项优化与改进,提升您的使用体验。",
"module_id": 1,
"category_id": 1,
"views": 150,
"thumb": "https://yourdomain.com/uploads/images/version-3.0.webp",
"link": "https://yourdomain.com/news/100.html",
// ... 其他文档信息
}
// ... 剩余3条最新文档
],
"msg": ""
}
In the returneddataarray, each item is a document object. You can easily extract it fromtitle(Title),description(Summary),thumb(thumbnail),link(Document link) and present it on your website or application interface.created_timeandupdated_timeThe field is returned in the form of a Unix timestamp, you can convert it to a readable date format.
Practical Tips and **Practice**
- clear
moduleId:In your AnQiCMS backend, go to the 'Content Model' management interface to view each model's ID. Make sure you are usingmoduleIdis correct. - Flexible application
orderparameters:exceptcreated_time desc, and you can also tryupdated_time descGet the latest modified document orviews descGet the popular documents. - Moderate setting
limit:Reasonably set according to the page layout and content display requirementslimitAvoid making too many requests at once to prevent affecting loading speed. - Error handling mechanism:When integrating API calls in front-end or back-end code, be sure to check the returned
codefield. Ifcodenot0means that the request failed, at this point you canmsgget the error reason through the field and take appropriate handling or prompting.
By the above method, you can easily use the AnQiCMS API to get the latest document list under any specified content model, injecting vitality into your website or application.
Frequently Asked Questions (FAQ)
Q1: How do I know my content model?moduleIdWhat is it?A1: You can find it in the AnQiCMS admin interface.moduleId. Usually in the 'System Settings' or 'Content Model' related menus, each model will display its corresponding ID.For example, when clicking on the edit page of a content model, the model ID is usually marked in the URL or next to the model name.You can also call by using/api/module/listThe interface retrieves the list of all content models, which includesidandtitlefield.
Q2: If I need to get the latest documents under a certain category (not the entire model), what should I do?A2: If you only want to retrieve the latest document in a specific category, you canarchive/listadd an extra parameter in the interfacecategoryIdand set its value to the ID of the target category. For example,GET {您的域名}/api/archive/list?moduleId=1&categoryId=10&order=created_time desc&limit=5It will retrieve the latest 5 articles with model ID 1 and category ID 10.You can find the corresponding category ID in the "Category Management" of the AnQiCMS backend.
Q3: How to ensure that my website can continuously obtain the latest content without causing problems due to too frequent API requests?A3: It is recommended to implement a caching mechanism on your server or frontend (depending on the actual situation) for the frequently accessed 'latest documents' list.For example, request the API to update data every 5-10 minutes, and then cache the data for user access, rather than directly requesting the API each time the page is loaded.This can significantly reduce the pressure on API requests and improve page loading speed.