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 with 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, optimizing the website's user experience.

To implement this function, we mainly use the AnQiCMS provided/api/archive/listDocument list interface. This interface is designed to be very flexible, allowing us to precisely filter, sort, and obtain the required content by combining different parameters.

Understandingarchive/listInterface

Firstly, let's understand this core interface. Its call address is usually{域名地址}/api/archive/list, and proceed throughGETto send the request. Here{域名地址}should be replaced with your actual website domain, for examplehttps://en.anqicms.com/api/archive/list.

This interface has many parameters, but in order to getthe latest document list under the specified model, we need to pay special attention to the following:

  1. 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.moduleIdWe tell the system which model's documents we want to retrieve.
  2. order(Sorting method)This is the key to achieving the 'latest' effect. AnQiCMS supports sorting by multiple fields, including those related to time.created_time(Publish time) andupdated_time(Update time). To get the latest content, we will set it tocreated_time descorupdated_time descwhere,descmeans in descending order, that is, the latest one at the front.
  3. limit(Number of displayed items)This parameter is used to control how many of the latest documents you want to retrieve. For example, if you want to display the latest 5 articles, you can setlimit=5If you need to retrieve a certain number of data starting from a specific record, you can also useoffset,limitformat, for examplelimit=2,10means to retrieve 10 records starting from the 3rd one.
  4. type(list type): By default,typehas a value oflistit will directly return the specified number of documents. If you need to display pagination (such as showing the total number of pages or total count), then you cantypesetpage, at this time,配合pageThe parameter can retrieve the content of a specified page. However, for simple 'latest list' needs, keep the default.type=list.

Actual operation example

Assuming your 'article' model ismoduleIdYes1You now want to retrieve the latest 5 articles 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

When you send this GET request, AnQiCMS will return a JSON-formatted data. In a successful case,codethe field will be0whiledataThe field will include the list of documents 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 from ittitle(Title),description[Summary],thumb[Thumbnail],link[Document link] and other information, and display them on your website or application interface.created_timeandupdated_timeThe field is returned in Unix timestamp format, you can convert it to a readable date format.

Practical Tips and **Practice

  • ClearmoduleId:In your AnQiCMS backend, go to the 'Content Model' management interface to see each model's ID. Make sure you are using.moduleIdis correct.
  • Use flexiblyorderParameters:Exceptcreated_time desc, you can also tryupdated_time descTo get the most recently modified document, orviews descTo get the popular documents.
  • Moderate settingslimit:Reasonably set according to the page layout and content display requirementslimitAvoid making too many requests at once, which may affect loading speed.
  • Error handling mechanism:When integrating API calls in front-end or back-end code, be sure to check the returnedcodefields. IfcodeNot to0mean that the request failed, at this time you canmsgget the error reason from the field and take the corresponding handling or prompt.

By using the above method, you can easily retrieve the latest document list under any specified content model of AnQiCMS's API, injecting vitality into your website or application.


Common Questions (FAQ)

Q1: How do I know my content model?moduleIdWhat is it?A1: You can find it in the AnQiCMS admin interfacemoduleId.Each model will display its corresponding ID in the 'System Settings' or 'Content Model' related menus.For example, when editing a content model, the URL or next to the model name will usually indicate the model ID./api/module/listInterface to get the list of all content models, which will includeidandtitlefield.

Q2: How should I operate to get the latest document under a specific category (instead of the entire model)?If you only want to get the latest document under a specific category, you can archive/listadd an additional parameter in the interface categoryIdand 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=5Retrieve the latest 5 articles with model ID 1 and category ID 10.You can find the corresponding category ID in the "Category Management" section of the AnQiCMS backend.

Q3: How can I ensure that my website can continuously obtain the latest content without causing issues due to excessive API request frequency?A3: It is recommended to implement a caching mechanism on your server-side or front-end (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 loads.This can significantly reduce API request pressure while improving page loading speed.